gpt4 book ai didi

javascript - 从 AJAX 命令返回 JSON 格式的响应

转载 作者:行者123 更新时间:2023-11-29 19:30:13 26 4
gpt4 key购买 nike

如何让原生 javascript AJAX 返回 JSON 格式的响应而不是“基于 HTML 的文本”?

描述:在 jquery 中,以下 AJAX 函数返回 JSON 数据,这正是我们需要的。

JQUERY 代码

 //ajax Handler function through which I set default attribute and send request function defined separately to send request to server 
ajaxHandler = {
defaultAttributes: {
type: 'GET',
url: 'index.php/request',
datatype: 'json',
data: {},
success: null,
error: function(data) {
errorHandler.showError('An Error occurred while trying to retreive your requested data, Please try again...');
},
timeout: function() {
errorHandler.showError('The request has been timed out, Please check your Internet connection and try again...');
}
},
sendRequest: function(attributes) {
//i perform here through jquery
$.ajax(attributes);
}

现在,代码更改为 native javascript AJAX,我在其中发出“application/json;charset=UTF-8”请求,我得到的响应是“基于 HTML 的文本”而不​​是 JSON。

原生JavaScript

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
attributes.success(attributes.data);
}
}
xmlhttp.open(attributes.type, attributes.url);
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send((attributes.data));

在 Chrome 开发者工具中比较两者,在网络选项卡下,这是我为 Jquery AJAX 得到的:

Remote Address:127.0.0.1:80
Request URL:http://localhost/1412-DressingAphrodite/Webapp/index.php/request/getallfeatures
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:/
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Cookie:PHPSESSID=tivt0hi9oqtbtjem7m9d0emhr1
Host:localhost
Pragma:no-cache
Referer:http://localhost/1412-DressingAphrodite/Webapp/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36
X-Requested-With:XMLHttpRequest
Response Headersview source
Connection:Keep-Alive
Content-Type:application/json; charset="UTF-8"
Date:Fri, 30 Jan 2015 04:43:07 GMT
Keep-Alive:timeout=5, max=18
Server:Apache/2.4.10 (Ubuntu)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.12-2ubuntu4.1

这是我在 Native Javscript AJAX 中得到的:

Remote Address: 127.0.0.1:80
Request URL: http://localhost/1412-Dre/Webapp/index.php/request/getallfeatures
Request Method: GET
Status Code: 403 Forbidden
Request Headersview source
Accept: /
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Type: application/json;charset=UTF-8
Cookie: PHPSESSID=tivt0hi9oqtbtjem7m9d0emhr1
Host: localhost
Pragma: no-cache
Referer: http://localhost/1412-Dre/Webapp/ User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/39.0.2171.65 Chrome/39.0.2171.65 Safari/537.36
Response Headersview source
Connection: Keep-Alive
Content-Length: 1063
Content-Type: text/html
Date: Fri, 30 Jan 2015 06:40:26 GMT
Keep-Alive: timeout=5, max=90
Server: Apache/2.4.10 (Ubuntu)
X-Powered-By: PHP/5.5.12-2ubuntu4.1

请注意“Content-Length”值为 1063。我不确定这是否是问题所在。因此,重复一遍,我怎样才能让 native javascript AJAX 返回 JSON 格式的响应而不是“基于 HTML 的文本”?

最佳答案

如果您的响应是有效的 JSON,请尝试以下操作:

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var jsonResponse = JSON.parse(xmlhttp.responseText);

console.log(jsonResponse);
}
}

关于javascript - 从 AJAX 命令返回 JSON 格式的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com