jquery - 从 Jquery 调用 AWS API 时出现 CORS 错误
问题描述
从 Jquery 调用 AWS API 时出现 CORS 错误。
CORS 已在 API 端启用,我可以使用 react > Axios 进行调用,但我们必须为该应用程序使用 Jquery。
使用此代码:
$.ajax({
type: 'POST',
url: "https://awsapiurlhere",
crossDomain: true,
async:true,
data: {
aaa: "444",
bbb: "aaa",
ccc: "ccc"
},
dataType: 'JSON',
cors: true ,
contentType:'application/json',
headers: {
"Access-Control-Allow-Origin":"*",
},
success: function(response, status, xhr) {
if (response) {
console.log("The username you typed has been used!", status);
} else {
console.log("No responce" , status);
}
},
error: function(xhr, status, error) {
console.log("Something went wrong!", status );
}
});
得到
从源“ https://cdpn.io ”访问“ https://url ”处的 XMLHttpRequest已被 CORS 策略阻止:Access-Control-Allow-Headers 不允许请求标头字段 access-control-allow-origin在预检响应中。
解决方案
您正在发送 Access-Control-Allow-Origin 作为请求的一部分。根据错误消息,CORS 策略不允许请求标头。您不需要在请求中使用此标头,因为它是响应标头。从请求中删除此标头将解决此问题。
推荐阅读
- python - 从值不同的两个数据框中获取列
- arrays - 尝试将来自 API 的 JSON 数据显示为数组
- c# - 如何在 C# 中使用表单数据构造 HttpClient POST 请求?
- javascript - 按属性前缀提取对象
- r - 2列之间的差异
- python - 与示例 JWT 相比,获得不正确的 HMAC SHA256 签名
- java - 从 Dataflow 中的管道外部将变量传递给 ParDo
- scala - 设置 ActorSystem Akka (Scala)
- go - 尝试使用通道暂停和恢复 goroutine,但最终它们挂断了
- ios - 为什么 Firestore 请求挂在已发布的 iOS 应用程序中而不是 Expo 客户端中?