API 网关 CORS:没有“Access

编程入门 行业动态 更新时间:2024-10-28 12:23:37
本文介绍了API 网关 CORS:没有“Access-Control-Allow-Origin"标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

虽然已通过 API 网关设置 CORS 并且设置了 Access-Control-Allow-Origin 标头,但在 Chrome 中尝试从 AJAX 调用 API 时仍然收到以下错误:

XMLHttpRequest 无法加载

来自 OPTIONS 的回复:

如何在不恢复为 JSON-P 的情况下从浏览器调用我的 API?

解决方案

我遇到了同样的问题.我已经用了 10 小时来找出答案.

serverless/framework/docs/providers/aws/events/apigateway/

//handler.js'使用严格';module.exports.hello = 函数(事件,上下文,回调){常量响应 = {状态代码:200,标题:{"Access-Control-Allow-Origin" : "*",//需要 CORS 支持才能工作"Access-Control-Allow-Credentials" : true//需要 cookie、HTTPS 授权标头},body: JSON.stringify({ "message": "Hello World!" })};回调(空,响应);};

Although CORS has been set up through API Gateway and the Access-Control-Allow-Origin header is set, I still receive the following error when attempting to call the API from AJAX within Chrome:

XMLHttpRequest cannot load XXXXX.execute-api.us-west-2.amazonaws/beta/YYYYY. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.

I attempted to GET the URL through Postman and it shows the above header is successfully passed:

And from the OPTIONS reponse:

How can I call my API from the browser without reverting to JSON-P?

解决方案

I get the same problem. I have used 10hrs to findout.

serverless/framework/docs/providers/aws/events/apigateway/

// handler.js 'use strict'; module.exports.hello = function(event, context, callback) { const response = { statusCode: 200, headers: { "Access-Control-Allow-Origin" : "*", // Required for CORS support to work "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS }, body: JSON.stringify({ "message": "Hello World!" }) }; callback(null, response); };

更多推荐

API 网关 CORS:没有“Access

本文发布于:2023-11-24 19:08:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网关   API   Access   CORS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!