2

I see one similar question but it does not have an accepted response.

The following ajax request times out. But GET request on the same URL using browser or curl work fine. Note this is a cross domain AJAX since the code sits on a different server and URL is for AWS EC2 (elastbeanstalk) instance.

Any clues why?

$.ajax({
    url: "http://<edited>.elasticbeanstalk.com/api/v1/Location",
    dataType:'jsonp',
    crossDomain:true,
    timeout:120000
    }).done(function(){
        //do something
        $("#status").html("SUCESS");
    }).fail(function(jqXHR, textStatus){
        if(textStatus == 'timeout')
        {
            //alert('Failed from timeout');
            $("#status").html(textStatus);
            //do something. Try again perhaps?
        }
});

[EDIT] added

When I check the AWS server log I see the GET request is responded with a 200 (success). But still $.ajax request timesout

120.138.116.202 - - [17/Jun/2015:12:12:31 +0000] "GET /api/v1/Location HTTP/1.1" 200 144 "http://yyyy.xxxx.com/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36"

prasunnair
  • 92
  • 3
  • 12
  • Just to make it clear, I am seeing the .fail part of the code getting called and getting 'textStatus' as timeout – prasunnair Jun 17 '15 at 11:40

2 Answers2

0

You cannot handle errors for cross domain JSONP requests.

You have to use jsonp plug-in available on Github https://github.com/jaubourg/jquery-jsonp which gives you support for error handling and try to find out what exactly went wrong

You can go through their documentation which is simple and easy

Go through the jQuery Documentation. You will notice

error

A function to be called if the request fails (...) Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • More than handling the error, I am worried about why does it timeout for ajax when other methods of a GET request work fine. ANyway I will check the JSON plugin to see if the error response has more hints – prasunnair Jun 17 '15 at 11:38
  • yup...you should handle the error to know what exactly is the problem – Abhinav Jun 17 '15 at 11:40
0

[SOLUTION]

It turned out the issue was related to cross domain request. Updating server code to add 'Access-Control-Allow-Origin' header as per the link below solved the problem

Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource

Community
  • 1
  • 1
prasunnair
  • 92
  • 3
  • 12