David R
David R

Reputation: 15639

jQuery ajax - Absolute URL's and error handling

Is it possible to catch the HTTP errors (like 404, 500, 504 etc) when we call an external webservice by specifying an absolute url?. (like setting the url: attribute of $.ajax call to have a url value as http://api.geonames.org/findNearbyPostalCodes.

Right now I'm unable to receive any errors although firebug is catching them and showing it in the console.

Can someone help?

Here is my code.

$.ajax({
    type: 'GET',
    url: "http://api.geonames.org/findNearbyPostalCodes",
    data: '{"lat":47,"lng":"9","username":"demo"}',
    dataType: 'json',
    cache:false,
    async:false,
    statusCode:{
        404: function(){
            alert('Page not found');
        },
        500: function(){
            alert('Page not found');
        },
        504: function(){
            alert('Unknown host');
        }
    },
    success: function(data){
    alert(data);
         }
              error: function (xhr, exception, thrownError)
              {
                  alert(xhr.status);
              }
});

Upvotes: 1

Views: 4730

Answers (3)

JoeLinux
JoeLinux

Reputation: 4307

In your case, you cannot check the status code (assuming you're not making the request from api.geonames.org).

jQuery will always return a "0" as the status if the request is cross-domain:

$.ajax({
    type: 'GET',
    url: 'http://someotherdomain.com/api/query',
    dataType: 'json',
    data: '{"first": 1, "second": 2}',
    complete: function(response) {    // the 'response' object has the status code
        if (response.status == '200') {
            // do something on success
        } else if (response.status == '0') {
            alert('Your request is cross-domain');
        }
    }
});

If the request happens to be within the same domain, you'll get a valid status code that you can check (the complete attribute of the $.ajax() function will run after any success or failure callbacks are run).

Upvotes: 0

jfriend00
jfriend00

Reputation: 707218

If using an absolute URL causes the domain to be different from the domain of your page (cross-domain request), then the only way to successfully execute an ajax call is to use JSONP which will cause the ajax library to use <script> tags for the cross-domain request instead of the more typical XMLHttpRequest used for same-domain requests.

You will not be able to intercept any sort of status codes from the loading of the cross-domain <script> tag.

Upvotes: 0

gilly3
gilly3

Reputation: 91487

No, it is not possible with cross-domain (external) requests using only client-side code. This is because cross-domain requests rely on JSONP - ie, injecting a script tag that loads code from an external source. Unfortunately, the <script> element does not have anything like an onerror event.

You can handle errors with same-domain requests because these typically use XMLHttpRequest, which returns a lot of useful information like status codes and response headers.

Your best bet would be to use your local server as a proxy.

Upvotes: 2

Related Questions