[英]CORS Error for Only POST request: AWS HTTP API Gateway
So my AWS HTTP API GATEWAY is integrated to a Lambda Function.所以我的AWS HTTP API GATEWAY被集成到一个Lambda Function。
Here's my CORS configuration On AWS.这是我在 AWS 上的 CORS 配置。
My GET & POST(without body) requests are working fine.我的 GET 和 POST(无正文)请求工作正常。
But when I make a POST request with some data/body, it has a CORS error.但是当我用一些数据/主体发出 POST 请求时,它有一个 CORS 错误。
Access to XMLHttpRequest at 'https://api_url/test' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
I understand how the browser sends an OPTIONS request before the Original POST request to the server.我了解浏览器如何在原始 POST 请求之前向服务器发送 OPTIONS 请求。
Here's the CORS error with the OPTIONS request.这是 OPTIONS 请求的 CORS 错误。
Here's how i call my request from a react app using axios.以下是我如何使用 axios 从反应应用程序调用我的请求。
const res_post = axios.post(`${process.env.REACT_APP_AWS_API_GATEWAY}/test`, {key: "value"}).then((res) => {
console.log("AXIOS POST REQUEST 2 RESULT", res, res.data);
});
I tried adding hearders to the axios call, No Luck.我尝试在 axios 呼叫中添加听众,运气不好。 I added a route(for / & /test) for a OPTIONS request, as suggested by some answers, unfortunately No luck.正如一些答案所建议的那样,我为 OPTIONS 请求添加了一条路线(for / & /test),不幸的是没有运气。
I have spent over 4 hours figuring out and applying all the solutions on stackoverflow.我花了 4 个多小时找出并应用了 stackoverflow 上的所有解决方案。 I will appreciate if someone can point me the right direction.如果有人能指出正确的方向,我将不胜感激。 I have read multiple stackoverflow questions and applied several of them.我已经阅读了多个 stackoverflow 问题并应用了其中的几个。
Refer to this: Vue & Axios + AWS API Gateway & Lambda - CORS + POST not working参考这个: Vue & Axios + AWS API Gateway & Lambda - CORS + POST not working
Even though I added a way to handle OPTIONS requests.即使我添加了一种处理 OPTIONS 请求的方法。 My lambda function was returning and empty response.我的 lambda function 正在返回并且响应为空。 I fixed that and it worked.我修好了,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.