jQuery将无法处理的应用程序/ JSON响应正确
问题描述:
简单的jQuery ajax调用和使用jQuery 1.8和Kohana的响应:jQuery将无法处理的应用程序/ JSON响应正确
$.ajax({
data:{
file: file
},
dataType: 'json',
url: 'media/delete',
contentType: 'application/json; charset=utf-8',
type: 'GET',
complete: function(response){
console.log(response);
console.log(response.file);
}
});
为URL的PHP是返回一个简单的json_encode()页:
{"file":"uploaded\/img\/Screen Shot 2012-04-21 at 2.17.06 PM-610.png"}
根据JSLint,哪个是有效的JSON。
的响应头,根据萤火虫:
Response Headers
Connection Keep-Alive
Content-Length 74
Content-Type application/json
Date Sun, 12 Aug 2012 17:44:39 GMT
Keep-Alive timeout=5, max=97
Server Apache/2.4.1 (Unix) PHP/5.4.0
X-Powered-By PHP/5.4.0
但在成功的函数“回应”不是JS对象我期待,我无法访问response.file。相反,它似乎是某种类型的响应对象,如readyState,responseText,状态等字段。
在这里有很多类似的问题,但我相信我已根据其他答案正确连接了一切。
我在这里错过了什么?
答
complete:
回调没有提供响应,因为它是参数。从jQuery doc for $.ajax()
:
complete(jqXHR, textStatus)
所以,你看到作为参数传递给你完整的功能是jqXHR对象,而不是解析响应。这很可能是因为在ajax调用完成时调用完成获取是否成功。这是success
处理程序获取成功解析的JSON响应。
您可能希望使用success:
回调代替。
$.ajax({
data: {file: file},
dataType: 'json',
url: 'media/delete',
contentType: 'application/json; charset=utf-8',
type: 'GET',
success: function(response){
console.log(response);
console.log(response.file);
}
});
哦哇,我觉得这是显而易见的,我只是看不到,谢谢。 – user1391445 2012-08-13 12:49:12