API Gateway CORS: no #39;Access-Control-Allow-Origin#39; header(API Gateway CORS:没有“Access-Control-Allow-Origin标头)
问题描述
虽然已经通过 API Gateway 设置了 CORS 并且设置了 Access-Control-Allow-Origin
标头,但在 Chrome 中尝试从 AJAX 调用 API 时仍然收到以下错误:
XMLHttpRequest 无法加载
并且来自 OPTIONS 响应:
如何在不恢复为 JSON-P 的情况下从浏览器调用我的 API?
我遇到了同样的问题.我已经用了 10 个小时来找出答案.
https://serverless.com/framework/docs/providers/aws/events/apigateway/
//handler.js'使用严格';module.exports.hello = 函数(事件、上下文、回调){常量响应 = {状态码:200,标题:{"Access-Control-Allow-Origin" : "*",//需要 CORS 支持才能工作"Access-Control-Allow-Credentials" : true//cookie 需要,HTTPS 授权标头},正文: 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 http://XXXXX.execute-api.us-west-2.amazonaws.com/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.
https://serverless.com/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 Gateway CORS:没有“Access-Control-Allow-Origin"标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:API Gateway CORS:没有“Access-Control-Allow-Origin"标头
基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01