Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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 检查axios库中是否返回404错误_Reactjs_Api_Axios - Fatal编程技术网

Reactjs 检查axios库中是否返回404错误

Reactjs 检查axios库中是否返回404错误,reactjs,api,axios,Reactjs,Api,Axios,404错误不会直接作为axios库中的res.status返回,而是作为错误抛出到catch中。 我现在的问题是,我是否可以在catch中查询错误号 .catch(error => { if(error.status === 404) // do something } 或者,我如何使用axios库来查询它是否为404错误 const handleLogin = () => { axios.post('localhost:4000', {&quo

404错误不会直接作为
axios
库中的
res.status
返回,而是作为错误抛出到catch中。 我现在的问题是,我是否可以在catch中查询错误号

.catch(error => {
if(error.status === 404)     
// do something     
}
或者,我如何使用axios库来查询它是否为404错误

const handleLogin = () => {
        axios.post('localhost:4000', {"email":`${email}`, "password":`${password}`})
          .then(res => {
            console.log(res);
            console.log(res.data);
            console.log(res.status);
            if(res.status === 200) {
              setValidationNoExist(true);
               history.push({
                pathname: '/app',
                state: {
                  email: email,
                  name: password,
                },
              })
            }
            else if(res.status === 203) {
              setValidationWrong(false);
            }
            else if(res.status === 404) {
                setValidationPending(false);
            }
             else if(res.status === 204) {
                setValidationNoExist(false);
              }
          })
          .catch(error => {
            console.log(error)
            setValidationNoExist(false);
        })
       };


您能否检查
error.response.status
是否为您的catch块提供了所需的值。

这是否回答了您的问题?