Axios在CORS中进行POST请求时,如果OPTIONS返回200,但POST返回500,则无法处理。

3

问题

Axios无法按照预期处理500错误。按照Axios错误处理提供的方式进行,但并不起作用。

POST请求发送用户名/电子邮件和密码,但密码故意设置为不正确,以测试错误处理。如果设置了正确的密码,则可以正常工作。

    axios.post(
        config.apiGateway.URL + ".../signin",
        payload
    ).then(response => {
        console.log(response)
        if(response.status == 200){
            console.log("Login successfull");
            this.setState({token: response.data.JWT_TOKEN});
        } else {
            console.log(response.status)
            this.setError()
        }
    }).catch(error => {
        this.setError()
        console.log(error)
        if (error.response) {
            console.log("--------------------------------------------------")
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
        } else if (error.request) {
            console.log("*************************")

            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
        } else {
            console.log("++++++++++++++++++++++++")
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
        }
        console.log(error.config);
    })

结果

预期执行 if (error.response) 部分,但实际执行的是 else if (error.request)

Access to XMLHttpRequest at 'https://***-api.us-east-1.amazonaws.com/dev/***/signin' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Login.js:73 Error: Network Error
    at createError (createError.js:17)
    at XMLHttpRequest.handleError (xhr.js:87)
Login.js:82 *************************
Login.js:87 XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}

在Chrome开发者工具中,OPTIONS方法返回如下的200状态码。
access-control-allow-headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token
access-control-allow-methods: GET,OPTIONS,POST,PUT
access-control-allow-origin: *
content-length: 0
content-type: application/json
date: Wed, 16 Jan 2019 01:32:48 GMT
status: 200
via: 1.1 ca8c566e53d4556421f22afbb9802d0c.cloudfront.net (CloudFront)
x-amz-apigw-id: Tkp2JEBmIAMFROA=
x-amz-cf-id: HXEEGh3TUcOckodVBKKoA1PVpg6h7UB5Ui3F1svTxpMYovB8Dt7rqQ==
x-amzn-requestid: a2144ccd-192e-11e9-9b6f-2dcad2a22d99

POST方法按预期返回500。

Request Method: POST
Status Code: 500 
Remote Address: 54.230.245.164:443
Referrer Policy: no-referrer-when-downgrade

环境

使用axios 0.18.0、Google Chrome 70.0.3538.77在Ubuntu 18 LTS中。

问题

请建议如何正确处理500错误。

研究

如何在Redux / Axios中捕获和处理422错误响应?看起来与错误被设置为堆栈跟踪以及响应似乎没有被填充的相同问题。

编辑: 我仍然不明白为什么记录错误会返回那个堆栈消息。我尝试像这样记录它。然后你可以看到它是一个对象。

1个回答

2

你的问题是否仅仅是为什么没有error.response

如果是这样,原因是处理你的POST请求的资源没有响应Access-Control-Allow-Origin头部,即使对于预检OPTIONS请求也是如此。

由于CORS策略,因此你的客户端代码被拒绝访问HTTP响应,因此error.response未定义。

在错误处理程序中,你可以假设空的response属性等同于网络级别问题。


非常感谢。将头部添加到500响应中解决了问题,MDN CORS(https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)也证明了这一点。 - mon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接