0

I am doing error handling in my.js, where i am having cross domain call to other server, also for dynamic HTML template i am using Mustache.js.

$.getJSON(url, function(data, textStatus, xhr) {
  $.each(data, function(i, data) {
    introPage(data);
  });
}).fail(function(Error) {
        alert(Error.status);
}); 

function introPage(data )
{
    $.getJSON('myphp.php?jsoncallback=?&template='+testTemplate, function(msg) {
        // my operations

    }).error(function(data) {
            }); 
}

We have .fail() in getJSON to catch the errors in getJSON. I am able to catch 404 error (not found) but when it comes to 406 error (not acceptable or say invalid input) or 401 error(unauthorised access), .fail() does not seems to work

but throws the error in console JSON ERROR IN CONSOLE

Also when i click the link. it shows error in jquery callback in below format

jQuery191014790988666936755_1378113963780({
  "error": {
    "code": 406,
    "message": "Not Acceptable: Sorry, There are no results to display for the given input."
  }
});

Now i am wondering how to get this error in my JS . i am not getting Error code 406 and 401.I need that error code so that appropriate error page be displayed.

Thank you

Salman A
  • 262,204
  • 82
  • 430
  • 521
Hitesh
  • 4,098
  • 11
  • 44
  • 82
  • You've been hit by, you've been struck by a smooth criminal. Also the [Same Origin Policy](http://en.wikipedia.com/wiki/Same-origin_policy) – Rory McCrossan Sep 02 '13 at 10:05
  • @RoryMcCrossan : i dont understand ??? even though there is two server involved but both are under same domain !!! than why Same origin policy. – Hitesh Sep 02 '13 at 10:19
  • the subdomains are different. For AJAX requests, the domains muct be EXACTLY the same - down to the protocol. – Rory McCrossan Sep 02 '13 at 10:22
  • @Rory: _Script and JSONP requests are not subject to the same origin policy restrictions._ – Salman A Sep 02 '13 at 10:24
  • @RoryMcCrossan : What i do understand is when i am making a call to my php 'url' using getJSON it is returning the error but it is not displaying me the error in .fail() because for some reason error is in jquery log or whatever .... so question how do i catch that error – Hitesh Sep 02 '13 at 10:25
  • @SalmanA obviously. But the OP is doing neither of those. – Rory McCrossan Sep 02 '13 at 10:26
  • also @RoryMcCrossan : thanks .... but can u explain little bit in simple words about Same-origin policy – Hitesh Sep 02 '13 at 10:26
  • @SalmanA : ur right :) – Hitesh Sep 02 '13 at 10:27
  • @hitesh that's because the request works, but is blocked by the security policies of the browser. Therefore the `fail()` handler is never hit. – Rory McCrossan Sep 02 '13 at 10:27
  • @SalmanA : jQuery v1.9.1 – Hitesh Sep 02 '13 at 10:28
  • @RoryMcCrossan : so how do we handle it?? – Hitesh Sep 02 '13 at 10:29
  • u mean something like this right $.getJSON(url, function(data, textStatus, xhr) { $.each(data, function(i, data) { introPage(data); }); }).done(function(success) { console.log(success); }); – Hitesh Sep 02 '13 at 11:11
  • @SalmanA : sorry i dnt how to format it in SO comments – Hitesh Sep 02 '13 at 11:12
  • @RoryMcCrossan : lets assume you are right than what is way out of it ??? how do i catch those errors – Hitesh Sep 02 '13 at 13:22

1 Answers1

1

Judging by the jsoncallback=? parameter and the response jQuery191...780({...}), it seems like jQuery is generating a JSONP request. JSONP requests are not subject to the same origin policy restrictions but they do have the following limitation as described here:

Note: This [error] handler is not called for cross-domain script and cross-domain JSONP requests.

The solution:

  1. Create a pure JSON request if the server allows cross-domain requests
  2. Look at answers for these questions:
  3. Use jquery-jsonp -- it does not give you the HTTP error code
Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • if i remove the jsoncallback=? ....... i am getting error XMLHttpRequest cannot load http://example.com/myphp.php?&template=introPage.html. Origin http://myLocal is not allowed by Access-Control-Allow-Origin. ....... – Hitesh Sep 02 '13 at 13:07
  • I am afraid this is because of _Same Origin Policy_ in which case you must use JSONP in which case you cannot check for HTTP status. – Salman A Sep 02 '13 at 14:05
  • so is there no way out of this ... i mean there should be something we can do .... there is always a solution to problem ?? @Salman A – Hitesh Sep 02 '13 at 14:09
  • If you control the other script (the one which sends JSON data or throws 404 errors) you can simply add [this header](https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS#Access-Control-Allow-Origin) to that script: `Access-Control-Allow-Origin: *`. The other solution is to host a cross-domain proxy script on your server. – Salman A Sep 02 '13 at 18:13