当请求失败时,Jquery不处理HTTP错误

时间:2021-06-21 15:34:52

My problem is "simple". I'm developing a hybrid Android application that makes HTTP requests via Jquery Ajax. The problem is that when my requests gets failed(Server returns 401/403 ...etc), Jquery fires only HTTP 404 error that is not correct.

我的问题是“简单”。我正在开发一个混合Android应用程序,它通过Jquery Ajax发出HTTP请求。问题是,当我的请求失败时(服务器返回401/403等),Jquery只会触发不正确的HTTP 404错误。

I'm making HTTP post requests like that:

我正在发布这样的HTTP帖子请求:

    var request = $.ajax({
    url: url,
    type: 'POST',
    dataType: 'json',
    data: obj,
    cache: false,
    contentType : 'application/json',
    beforeSend : function(xhr) {
        xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
    },  
    success: function(data){
        // Success
    },
    error: function(request, status, errorThrown){

        console.log("call Login ajax failed with error, status:" + status + " errorThrown:" + errorThrown);
        console.log("call Login ajax failed with errorCode : " + request.status);

    }

The strange thing is that when i run my project as DynamicWeb project in my browser at my PC and in the url use my ip address the jquery fires the correct error events, but when i use "localhost" in the url, the jquery act as in the android devices, fires only 404 event. I tried to change jquery libary to a newest version 2.1.1 but this doesn't help. PS. I'm using jquery 2.0.2...

奇怪的是,当我在我的PC上的浏览器中运行我的项目作为DynamicWeb项目并在url中使用我的IP地址时,jquery会触发正确的错误事件,但是当我在url中使用“localhost”时,jquery充当在Android设备中,只发生404事件。我试图将jquery libary更改为最新版本2.1.1,但这没有用。 PS。我正在使用jquery 2.0.2 ......

2 个解决方案

#1


0  

You'll need to set the header of the response to Content-type: application/json.

您需要将响应的标头设置为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: url,
  headers: { "Accept-Encoding" : "gzip" }, // Use the response seen in sniffer
  type: 'POST',
  dataType: 'json',//be sure you are receiving a valid json response or you'll get an error
})
.done(function(response) {
  console.log("success");
  console.log(response);
})
.fail(function() {
  console.log("error");
})
.always(function() {
  console.log("complete");
});

#2


0  

The problem for me was that server doesn't allow Cross-domain requests. So i must set "Access-Control-Allow-Origin:", "*" header in responses from my server and everything will be ok...

对我来说问题是服务器不允许跨域请求。所以我必须在我的服务器的响应中设置“Access-Control-Allow-Origin:”,“*”标题,一切都会好的......

#1


0  

You'll need to set the header of the response to Content-type: application/json.

您需要将响应的标头设置为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: url,
  headers: { "Accept-Encoding" : "gzip" }, // Use the response seen in sniffer
  type: 'POST',
  dataType: 'json',//be sure you are receiving a valid json response or you'll get an error
})
.done(function(response) {
  console.log("success");
  console.log(response);
})
.fail(function() {
  console.log("error");
})
.always(function() {
  console.log("complete");
});

#2


0  

The problem for me was that server doesn't allow Cross-domain requests. So i must set "Access-Control-Allow-Origin:", "*" header in responses from my server and everything will be ok...

对我来说问题是服务器不允许跨域请求。所以我必须在我的服务器的响应中设置“Access-Control-Allow-Origin:”,“*”标题,一切都会好的......