LoginSignup
0
0

More than 1 year has passed since last update.

ReactのaxiosでAWS lamdaをたたいたらCORSエラー

Posted at

この前からAWS SAMにと取り組んでます。AWS上にlamda関数は構築できてブラウザからアクセスでは正常に帰ってくるけど、reactのaxiosで叩いたら、でました。いつも引っかかるやつ。CORSエラーです。

Access to XMLHttpRequest at 'http://localhost:3000/hello' from origin 'http://localhost:3001' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

下記記事に助けられました、localでも同じです。かんしゃかんしゃ。

lamda関数の返り値に以下の内容でheaderを付与します。

    return {
        'statusCode': 200,
        'headers': {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "POST,GET,PUT,DELETE",
            "Access-Control-Allow-Headers": "Content-Type"
        },
        'body':json.dumps({
            "message":"hello world"
        })

これでaxiosを使ってlamdaを実行できました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0