Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 从请求头获取401状态代码_Reactjs_Axios - Fatal编程技术网

Reactjs 从请求头获取401状态代码

Reactjs 从请求头获取401状态代码,reactjs,axios,Reactjs,Axios,我面临使用react中的axis库检索未经授权请求的响应状态代码的问题。下面是代码段日志200,但不是401 axios({ url: url, method: 'get', // default\ timeout: 10000, responseType: 'json', // default maxContentLength: 2000, }).then((response

我面临使用react中的axis库检索未经授权请求的响应状态代码的问题。下面是代码段日志200,但不是401

axios({
           url: url,
           method: 'get', // default\
           timeout: 10000,
           responseType: 'json', // default
           maxContentLength: 2000,
     }).then((response) => {
    console.log("Status", response.status); //logs 200 but not 401
    })
我知道将不会发送401的响应,但我可以在请求的标题中看到状态代码401。我不确定从请求头访问状态代码是否是个好主意,但因为并没有响应,所以并没有地方检查它。那么,知道如何从请求头中获取状态代码了吗?任何其他想法也将不胜感激

提前非常感谢。

400状态代码是被捕获的错误

axios({
    url: url,
    method: 'get', // default\
    timeout: 10000,
    responseType: 'json', // default
    maxContentLength: 2000,
    }).then((response) => {
        console.log("Status", response.status); //status code 200
    }).catch(e=>{
        console.log("Status", e.response.status); //status code 300 and 400
})

检查axios的处理错误。

此处的“e”上没有定义状态。我确实尝试过这个,但它给了我一个未定义的。感谢nrgwsth共享文档,我尝试了axiom.get选项,我能够获得状态。但是如果我使用axios.get,问题是如果我使用axis.get,我无法配置请求属性,例如超时、响应类型、maxContentLength等。我可以很容易地以axis{url:url,method:get,timeout:1000….}格式进行配置,为此,catch不返回状态。