Pregunta

I have an ajax call as follows-

this.send({
    url : AppConstants.variables.callURL.feed,
    data : rqStr,
    onSuccess : function(data, textStatus) {
           console.log(textStatus);
});

send : function(obj) {
    this.callbackObj = obj;
    FeedBackPanel.add(obj.url + "->" + obj.data);
    $.ajax({
        url : obj.url,
        data : obj.data,
        dataType : "jsonp",
        jsonp : "callback",
        jsonpCallback: "RequestController.callbackObj.onSuccess"
    });
}

It works fine>however i am trying to provide a wrong domain and in that case i get a

Status Code:504 Gateway Time-out

in the chrome network developer panel.However,Console.log doesn't give me that status.So is there any way i can catch the status code,so that i can take appropriate actions.

My code is working fine..I just need to catch the 504 Gateway Time-out message.Plz help.

¿Fue útil?

Solución

Due to the way jsonp requests works, you are not able to find out if a request failed and why.

What you can do is to set a timeout yourself for the jsonp request. If you don't get a valid response in that time, the error callback is called.

You need to figure out yourself what value you want to set for the timeout, because if e.g. the time required for sending the request, building the response and sending it back takes 500ms and you set the timeout to 100ms, the error callback will be called all the time, even if the request itself did not timeout.

$.ajax({
  url : obj.url,
  data : obj.data,
  dataType : "jsonp",
  jsonp : "callback",
  timeout:300,
  jsonpCallback: "RequestController.callbackObj.onSuccess"
}).success(function() {
  console.log('test')
}).error(function() {
  console.log("error");
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top