3

I am performing one cross domain get operation as shown below.

$.ajax({
    type: "GET",
    url: "http://localhost:65249/api/item/get",
    data: {
        searchText: "test"
    },
    dataType: "jsonp",
    async: false,
    success: function (results) {
        alert(results);
    },
    error: function (jqXHR, error, errorThrown) {
        if (jqXHR.status && jqXHR.status == 401) {
            alert("Unauthorized request");
        } else if (jqXHR.status && jqXHR.status == 404) {
            alert("The requested page not found");
        }
    }
});

But success or error block is not getting called after request is completed. when i debug java script in developer console i am receiving error but error block of javascript is not getting called.

GET http://localhost:65249/api/item/getallproducts?callback=jQuery182028460139059461653_1396510235829&searchText=test&_=1396510674779 401 (Unauthorized)      
Michał
  • 2,456
  • 4
  • 26
  • 33
Sandeep
  • 33
  • 1
  • 6

2 Answers2

2

Unfortunately, if you are using JSONP, all requests that return an error fail silently. This is because JSONP uses a script tag instead of XmlHttpRequest. If you want errors to fire, you need to use XHR with CORS. CORS needs to be configured on the server side, and it works client side only in IE 10+.

Tommi Gustafsson
  • 1,860
  • 4
  • 17
  • 20
  • i have removed "jsonp" from ajax call and configured cors in web.cofig of web api application and now error block of jquery working fine. – Sandeep Apr 03 '14 at 08:43
0

error dont work on corss domain calls, see jquery doku. for error:

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

Take a look at this answer: JSONP request error handling

Community
  • 1
  • 1
reyaner
  • 2,799
  • 1
  • 12
  • 17