jQuery AJAX GET/POST request returns 404 in error handler, but valid response is sent from server

▼魔方 西西 提交于 2019-12-05 16:27:55

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");
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!