Jquery ajax gateway handling error

I am doing cross domain 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 the success or error block is not called after the request completes. when i debug java script in developer console i get error but javascript error code is not called.

GET http://localhost:65249/api/item/getallproducts?callback=jQuery182028460139059461653_1396510235829&searchText=test&_=1396510674779 401 (Unauthorized)      

      

+2


source to share


2 answers


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



+1


source


the error does not work for domain corss 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: Handling JSONP Request Errors

0


source







All Articles