Lambda CORS错误在客户端被阻止,但在邮递员中有效

时间:2020-03-04 16:10:12

标签: reactjs lambda cors axios

我知道有很多与此主题相关的主题,并且相信我,目前我已经研究了所有主题。

这是确切的错误:

Access to XMLHttpRequest at 'https://8udb877nfi.execute-api.us-east-1.amazonaws.com/prod/bulk-updater' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我向邮递员请求该端点时,一切正常,但是当我从客户端发出请求时,出现上述错误。

这是我在lambda中的处理程序:

exports.handler = async (event, context, callback) => {
    var responseCode = 200;

    var response = {
        statusCode: responseCode,
        headers: {
            "x-custom-header" : "my custom header value",
            "Access-Control-Allow-Origin": "*"
        },
        body: JSON.stringify(event)
    };
    callback(updateId(event), response)
}

这是在React Client中设置我的请求的方式:

const requestToRtpManager = (idObjects) => {
    axios({
        method: 'POST',
        url: 'https://8udb877nfi.execute-api.us-east-1.amazonaws.com/prod/bulk-updater',
        data: idObjects,
        headers: {
            'Access-Control-Allow-Credentials' : true,
            'Access-Control-Allow-Origin':'*',
            'Access-Control-Allow-Methods':'POST',
            'Access-Control-Allow-Headers':'application/json',
            'Content-Type': 'text/plain'
        },
    }).then(v=>console.log(v))
}

0 个答案:

没有答案