【问题标题】:Axios does not handle 500 from POST after OPTIONS returned 200 for CORS在 OPTIONS 为 CORS 返回 200 后,Axios 不处理来自 POST 的 500
【发布时间】:2019-06-10 02:16:41
【问题描述】:

问题

Axios 没有按预期处理 500。遵循Axios error handling 提供的方式,但它不起作用。

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

环境

在 Ubuntu 18 LTS 中带有 Google Chrome 70.0.3538.77 的 axios 0.18.0。

问题

请建议如何正确处理 500。

研究

How to catch and handle error response 422 with Redux/Axios? 似乎有同样的错误被设置到堆栈跟踪并且响应似乎没有被填充。

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

【问题讨论】:

    标签: javascript axios


    【解决方案1】:

    你的问题是不是很简单……

    为什么error.response 不在场?

    如果是这样,原因是处理您的 POST 请求的资源没有以 Access-Control-Allow-Origin 标头响应,即使它对飞行前 OPTIONS 请求进行响应。

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

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

    【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2020-06-23
    • 2017-11-09
    • 2015-11-04
    • 2019-06-04
    • 2012-12-22
    • 1970-01-01
    • 2018-12-23
    • 2018-10-23
    相关资源
    最近更新 更多