问题
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