ajax post returns Object { readyState=0, status=0, statusText=“error”}

眉间皱痕 提交于 2019-12-11 10:04:16

问题


I have a json string with me {offer.offer.offerId.USSellerId: {$gt: 50}}. All i wanted to do was to call a POST REST Service with this json query as payload and get the returned JSON.

I was able to call that service using chrome advanced rest client and I get the data back. But my problem is when I tried to call this service using below line of code I get the following errors

    $.ajax({
        url:"http://10.242.172.12:8080/cqs/services/services/cqs/search",
        Accept: "application/json",
        type: "POST",
        contentType: "application/json",
        data: {"offer.offer.offerId.USSellerId": {"$gt": 50}},
        async:true,
        crossDomain: true,
        always: function(data, textStatus, errorThrown) {
         console.log(data)
         console.log(textStatus)
        },
        success: function (data) {
            console.log(data)
            alert(data);
        },
        error: function (data, status, err) {
            console.log(data)
            console.log(status)
            console.log(err)
        }
    });

I see the following in the console:

Object { readyState=0, status=0, statusText="error"} error emptry string..

I could not understand anything from this error. When i did google this error, many suggestion tell that it could be cross browser problem. I have no control over server side code.

How can i fix that cross browser error from client side?


回答1:


Please check the XMLHttpRequests being stopped:

If you end up with an XMLHttpRequest having status=0 and statusText=null, it means that the request was not allowed to be performed. It was UNSENT. A likely cause for this is when the XMLHttpRequest origin (at the creation of the XMLHttpRequest) has changed when the XMLHttpRequest is then open(). This case can happen for example when one has an XMLHttpRequest that gets fired on an onunload event for a window: the XMLHttpRequest gets in fact created when the window to be closed is still there, and then the request is sent (ie open()) when this window has lost its focus and potentially different window has gained focus. The way to avoid this problem is to set a listener on the new window "activate" event that gets set when the old window has its "unload" event fired.



来源:https://stackoverflow.com/questions/25818224/ajax-post-returns-object-readystate-0-status-0-statustext-error

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