简体   繁体   English

在ajax调用后获取http状态代码

[英]Get http status code after ajax call

Im trying to get the http status code of my ajax call using the below code but the xhr.status is always undefined.. 我试图使用以下代码获取我的ajax调用的http状态代码,但xhr.status始终未定义。

  function checksession() {
            $.ajax({
                type:'POST'
                ,url:'CheckSession'
                ,success: validateresult(session_ind)
                ,data: { antiCSRF : '{{acsrf}}',
                       session_id: '{{session_id}}'}
                })
        function validateresult(session_ind,xhr){
                alert(xhr.status)
            }
$.ajax({
    //...        
    success: function(data, textStatus, xhr) {
        console.log(xhr.status);
    },
    complete: function(xhr, textStatus) {
        console.log(xhr.status);
    },
    error: function(xhr, textStatus , xhr) {
        console.log(xhr.status);
    }  
});

or use : 或使用:

statusCode : An object of numeric HTTP codes and functions to be called when the response has the corresponding code. statusCode :一个数字HTTP代码对象,当响应具有相应代码时将调用该对象。 For example, the following will alert when the response status is a 404: 例如,以下内容将在响应状态为404时发出警报:

$.ajax({
   statusCode: {
      404: function() {

      }
   }
});
 function checksession() {
        $.ajax({
            type:'POST'
            ,url:'CheckSession'
            ,success: validateresult(session_ind)
            ,data: { antiCSRF : '{{acsrf}}',
                   session_id: '{{session_id}}'}
            success: function(data, textStatus, xhr) {
                console.log(xhr.status);
            },
            complete: function(xhr, textStatus) {
                console.log(xhr.status);
            },
            error: function(xhr, textStatus , xhr) {
                console.log(xhr.status);
            } 
        });
}

Does not appear that xhr (or, jqxhr below) was included as argument to validateresult call , only session_ind 似乎没有包含xhr (或下面的jqxhr )作为validateresult call的参数,仅session_ind

Try 尝试

  function checksession() {
            $.ajax({
                type:'POST'
                ,url:'CheckSession'
                ,success: function (session_ind, textStatus, jqxhr) {
                   validateresult(session_ind, textStatus, jqxhr)
                }
                ,data: { antiCSRF : '{{acsrf}}',
                       session_id: '{{session_id}}'}
                });
        function validateresult(session_ind, textStatus, jqxhr){
                alert(jqxhr.status)
            }
  };
checksession();

jsfiddle http://jsfiddle.net/guest271314/nuukyLn0/ jsfiddle http://jsfiddle.net/guest271314/nuukyLn0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从 ajax 调用中获取 HTTP 状态代码 200 而不是 202 - How do i get HTTP status code 200 instead of 202 from an ajax call 使用jquery从ajax响应中获取Http状态代码 - Get Http Status Code from an ajax response with jquery jquery ajax http状态代码 - jquery ajax http status code jQuery $ .get / $。ajax传递HTTP状态代码200而不是预期的状态代码201或202 - jQuery $.get/$.ajax passes HTTP status code 200 instead of expected status code of 201 or 202 从此ajax调用获取http状态 - getting http status from this ajax call 当http状态代码为“200 OK”时,为什么$ .ajax调用json数据会触发错误回调? - Why does $.ajax call for json data trigger the error callback when http status code is “200 OK”? 给出错误HTTP状态代码“ 600”无效。 提交ajax后 - Giving error The HTTP status code “600” is not valid. after submitting ajax JQuery ajax调用获取状态代码0“错误” - JQuery ajax call getting status code 0 “error” $ .ajax(…)POST到Jersey WebService,无法获取HTTP错误详细信息(状态代码等) - $.ajax(…) POST to Jersey WebService, unable to get back HTTP error details (status code, etc) jQuery:如何从 $.ajax.error 方法中获取 HTTP 状态码? - jQuery: How to get the HTTP status code from within the $.ajax.error method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM