【问题标题】:How to get response status code from APIs in JS?如何从 JS 中的 API 获取响应状态码?
【发布时间】:2021-05-14 21:46:41
【问题描述】:

我正在调用 API 以通过 Ajax 登录。如果 Api 不成功,那么在错误函数中我需要获取 Api 返回的状态码。

            var weblink = "https://" + domain + username;
            
            $j.ajax({
            url: weblink,
            data: "", 
            type: 'GET',
            dataType: "xml",
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", "Basic " + encodedData);
            },
            
            crossDomain: true,
            
            success: function(result) {
            
            console.log(result);
            
            },
            
            error: function(jqXHR, textStatus, errorThrown) {   
                
                console.log("Status: " + jqXHR.statusCode);
                
            }
           });

但我无法获取状态码,我需要解析它还是什么?

【问题讨论】:

    标签: jquery ajax api response


    【解决方案1】:

    制作dataType:'JSON',

    并应用此代码:

    success:function(res){
        console.log(res)
    },
    error:function(e){
        console.log(e)
    },
    complete:function(c){
        console.log(c)
    }
    

    【讨论】:

    • dataType:'xml' 是 Api 的要求。
    • 你检查了吗,你会在控制台中得到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 2020-08-19
    • 2020-01-10
    • 2021-10-03
    • 2020-06-26
    • 1970-01-01
    相关资源
    最近更新 更多