处理跨域jquery ajax调用时出错

我正在执行一个跨域get操作,如下所示。

$.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"); } } }); 

但是在请求完成后不会调用成功或错误块。 当我在开发人员控制台中调试java脚本时,我收到错误但是没有调用javascript的错误块。

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

不幸的是,如果您使用的是JSONP,则所有返回错误的请求都会以静默方式失败。 这是因为JSONP使用脚本标记而不是XmlHttpRequest。 如果要激发错误,则需要将XHR与CORS一起使用。 需要在服务器端配置CORS,它仅在IE 10+中的客户端工作。

错误不适用于corss域调用,请参阅jquery doku。 错误:

注意:不会为跨域脚本和跨域JSONP请求调用此处理程序。

看一下这个答案: JSONP请求error handling