jQuery的AJAX GET / POST请求返回404错误处理,但有效的响应从服务器发送错误、有效、服务器、AJAX

2023-09-10 19:49:27 作者:暖葵

问题:我发送GET或POST请求与jQuery.ajax并获得与404的错误处理程序引发的事情是,我已经使用嗅探器来检查来自服务器的应答时,这是一个有效的200响应与正确的JSON(被Python json.dumps返回)。 而奇怪的事情:在此之后通话结束了404浏览器重新加载页面。

一切都住在同一个域(和子太)。

jQuery的电话:

  $阿贾克斯({类型:POST,网址:/ M /认证/登陆,数据:{X:Y},
成功:函数(结果){}错误:函数(XHR){},数据类型:JSON});
 

响应与有效载荷看到嗅探器DECOM pressed:

  HTTP / 1.1 200 OK \ r \ñ
内容类型:text / html的;字符集= UTF-8 \ r \ñ
缓存控制:无缓存,必重新验证\ r \ñ
内容编码:gzip \ r \ñ
的X AppEngine上,估计-CPM-US-美元:$ 0.000018 \ r \ñ
的X的AppEngine服务资源用法:毫秒= 71 cpu_ms = 0 \ r \ñ
有所不同:接受编码\ r \ñ
日期:星期二,2013年12月24日21时零四分36秒GMT \ r \ñ
杂注:无缓存\ r \ñ
到期日:周五,1990 00:00:00 GMT \ r \ñ01一月
服务器:谷歌前端\ r \ñ
内容长度:80 \ r \ñ
复用协议:80:QUIC,80:QUIC \ r \ñ
\ r \ñ
{理由:{密码:失踪,电子邮件:失踪},错误:参数}
 

解决方案

与内容类型的服务器响应:文字\ html和你的Ajax调用预计JSON。你需要设置响应内容类型的标题:应用程序/ JSON。你也可以尝试设置你的Ajax调用的头接受gzip的EN codeD数据。

在PHP中,你可以做到这一点。

  

标题(内容类型:应用程序/ JSON');

jQuery基础 Ajax,load ,getJSON ,getScript ,post ,ajax ,同步 异步请求数据 ...

您呼应的JSON之前。或者你可以设置Ajax调用文本,HTML类型。

也被使用jQuery像这样建议链中的回调函数

  $。阿贾克斯({
    网址:/ M /认证/注册,
    标题:{接受编码:GZIP},
    键入:POST,
    数据类型:'json的',//确保您收到有效的JSON响应,否则你会得到一个错误
    数据:{X:Y},
})
.done(函数(响应){
    的console.log(成功);
    的console.log(响应);
})
.fail(函数(){
    的console.log(错误);
})
。总是(函数(){
    的console.log(完成);
});
 

The Problem: I send GET or POST request with jQuery.ajax and get an error handler triggered with 404. The thing is, I have used a sniffer to check an answer from server and it is a valid 200 response with a correct JSON (returned by Python json.dumps). And something weird: after this call ends up with 404 a browser reloads the page.

Everything lives in the same domain (and subdomain too).

jQuery call:

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

Response as seen by sniffer with payload decompressed:

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"}

解决方案

the server response with content-type: text\html and your ajax call expects json. you'll need to set the header of the response to Content-type: application/json. also you might try setting the headers of your ajax call to accept gzip encoded data.

in php you could do this

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

before you echo the json. Or you could set the type of the ajax call to text, html.

also it is recommended to chain your callback functions with jquery like so

$.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");
});