jQuery AJAX GET / POST请求在error handling程序中返回404,但是从服务器发送了有效响应

问题:我用jQuery.ajax发送GET或POST请求,并获得用404触发的error handling程序。问题是,我使用嗅探器检查服务器的答案,它是一个有效的200响应,具有正确的JSON(返回通过Python json.dumps)。 而且有些奇怪:在这次调用结束后,浏览器会重新加载页面。

一切都生活在同一个域(也是子域)。

jQuery调用:

$.ajax({type: "POST", url: "/m/auth/login", data: {x: "y"}, success: function(result) {}, error: function(xhr) {}, dataType: "json"}); 

具有有效负载解压缩的嗅探器看到的响应:

 HTTP/1.1 200 OK\r\n Content-Type: text/html; charset=utf-8\r\n Cache-Control: no-cache, must-revalidate\r\n Content-Encoding: gzip\r\n X-AppEngine-Estimated-CPM-US-Dollars: $0.000018\r\n X-AppEngine-Resource-Usage: ms=71 cpu_ms=0\r\n Vary: Accept-Encoding\r\n Date: Tue, 24 Dec 2013 21:04:36 GMT\r\n Pragma: no-cache\r\n Expires: Fri, 01 Jan 1990 00:00:00 GMT\r\n Server: Google Frontend\r\n Content-Length: 80\r\n Alternate-Protocol: 80:quic,80:quic\r\n \r\n {"reason": {"password": "missing", "email": "missing"}, "error": "argument"} 

带有content-type:text \ html的服务器响应和你的ajax调用需要json。 您需要将响应的标题设置为Content-type:application / json。 您也可以尝试设置ajax调用的标头以接受gzip编码数据。

在PHP中你可以做到这一点

header(’Content-type:application / json’);

在你回应json之前。 或者您可以将ajax调用的类型设置为text,html。

另外,建议用jquery链接你的回调函数

 $.ajax({ url: '/m/auth/login', headers: { "Accept-Encoding" : "gzip" }, type: 'POST', dataType: 'json',//be sure you are receiving a valid json response or you'll get an error data: {x: 'y'}, }) .done(function(response) { console.log("success"); console.log(response); }) .fail(function() { console.log("error"); }) .always(function() { console.log("complete"); });