从 AJAX 命令返回 JSON 格式的响应
Return response in JSON from AJAX command
如何让原生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);
}
现在,代码更改为本机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 Developer Tools中比较两者,在"网络"选项卡下,以下是我对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
请注意"内容长度"值 1063。我不确定这是否是问题所在。那么,重复一遍,我怎样才能让原生javascript AJAX以JSON而不是"HTML文本"返回响应?
如果您的响应是有效的 JSON,请尝试以下操作:
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
var jsonResponse = JSON.parse(xmlhttp.responseText);
console.log(jsonResponse);
}
}
你可以试试responseType
xmlhttp.responseType = 'json'
但首先检查浏览器支持。
更多信息
从服务器收到 json 文本后,您始终可以执行以下操作:
var jsonStr = "<<your_json_string_response>>";
var jsonObject = eval('('+jsonStr+')');
相关文章:
- 将json响应格式化为父级和子级
- 将服务器响应格式化为人类可读
- RSS提要响应不是以原始XML格式提供的
- 响应出现JSON格式问题
- 如何发布到URL并打开一个新窗口以显示pdf格式的响应
- 为什么我的rails应用程序在生产环境中没有正确响应.js格式
- 来自Mongo的JSON格式的无阻塞写入响应
- jQuery ajax 调用 - jim 脚本响应应该是什么格式
- 从 AJAX 命令返回 JSON 格式的响应
- 强制 Chrome 显示格式为树结构的 Json 响应
- JavaScript JSON 响应格式
- 使用 Jquery 解析 Json 格式的 ajax 响应
- 从aspx页面获取json格式的响应,而不是纯html格式
- Javascript自定义函数,用于将JSON格式的YQL查询响应导入谷歌电子表格
- 以HTML格式操作AJAX响应
- JSONP响应格式错误
- 将当前页面数据存储在一个变量中,其格式/结构与Ajax html响应相同
- 响应格式与rails中的请求不同
- 获取XML格式的响应数据
- NODEJS无法发送json格式的响应