无法在jquery AJAX调用中检测403状态代码

我使用以下代码在端口9084上调用另一台用户计算机上的应用程序(缺少DEV服务器)。 这是一个JAVA应用程序,我想从我的ASP.Net应用程序调用它(它使用母版页)。 我想调用下面的URL,它将在cookie中返回403错误状态和会话ID。 我想使用此会话ID并使用应用程序ID和密码进行另一次调用。

当我在浏览器中使用URL“http:fwdslash fwdslash machinename:9084 fwdslash PropertyInsuranceDB fwdslash propertyAssociation fwdslash ajax fwdslash getPolicyAddress fwdslash H0271812 fwdslash 08”时,我收到HTTP 403 Forbidden错误页面。 当我尝试下面的代码时,我在Fiddler中看到了相同的内容,但是我无法在下面的ajax调用的错误部分中捕获它。

我能做错什么? 任何建议将不胜感激。

$.ajax({ url: "http://machinename:9084/PropertyInsuranceDB/propertyAssociation/ajax/getPolicyAddress/H0271812/08", context : document.body, type: "get", success: function (data, status) { alert(status); }, error: function (xhr, desc, err) { alert(xhr.status); alert(desc); alert(err); } }); 

谢谢

–Nivedita

如果服务器返回一些内容,它通常被认为是一个成功的ajax调用,即使它没有返回你想要它返回的内容。

要捕获状态代码,您可以执行

  $.ajax({ url: "YOURURLHERE", context: document.body, type: "get", statusCode: { 403: function (xhr) { console.log('403 response'); } }, success: function (data, status) { alert(status); }, error: function (xhr, desc, err) { alert(xhr.status); alert(desc); alert(err); } }); 

如果你想全局地做,jquery给它和它的API( 这里的文档 )。 有些人建议使用ajaxSetup更改默认配置,但jquery不鼓励它 。 所以首选方法就像这样:

 $( document ).ajaxComplete(function( event, xhr, settings ) { if(xhr.status == 403){//unauthorized calls // do whatever want with the event ie: //event.stopImmediatePropagation(); //show a global UI for ajax login again console.error('403 response'); } });