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

时间:2021-12-18 07:34:30

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.

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

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

1 个解决方案

#1


1  

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.

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

in php you could do this

在PHP中你可以做到这一点

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

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

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

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

另外,建议用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");
});

#1


1  

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.

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

in php you could do this

在PHP中你可以做到这一点

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

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

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

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

另外,建议用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");
});