如何使用React js向AWS Api发出请求

时间:2019-02-21 13:04:34

标签: reactjs aws-lambda amazon-dynamodb aws-api-gateway

我已经使用React js使用get方法向aws api发出请求,但是它显示错误:-  “从源'https://blahblah.com/dev/getControllist到'http://test.com:3000'处获取的访问已被CORS策略阻止:在以下位置,Access-Control-Allow-Headers不允许请求标头字段access-control-allow-origin飞行前反应。” 但是,如果我直接将同一个api击中浏览器或使用邮递员,它将成功返回json。如果有人知道,请帮助我。 我的代码是:-

var uri = "blahblah.com/dev/getControllist";
    let h = new Headers({
        'Access-Control-Allow-Origin': '*',
        "Access-Control-Allow-Credentials": true,
        'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
        "Access-Control-Request-Headers": "Origin, X-Requested-With, Content-Type, Accept,x-access-token"

        });       


    let request = new Request(uri,
        {
            method: 'GET',
            headers: h,
            cors:true
        });

    fetch(request)
        .then((result) => {
            // Get the result
            // If we want text, call result.text()              

            return result.json();

        }).then((jsonResult) => {
            // Do something with the result
            console.log(jsonResult);
            this.setState({
                items: jsonResult
            });

0 个答案:

没有答案
相关问题