javascript - GetResponse Api 集成使用 fetch() 不起作用
问题描述
GetResponse Api 集成使用 Fetch API 方法不起作用
控制台中显示以下 SMS:
Cross-Origin Request Blocked:
The Same Origin Policy disallows reading the remote resource at https://api.getresponse.com/v3/contacts.
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
代码如下:
// main.js
// POST request using fetch()
fetch("https://api.getresponse.com/v3/contacts", {
// Adding method type
method: "POST",
// Adding body or contents to send
body: JSON.stringify(
{
campaign : {
campaignId: "5D8Qm"
},
name: "xyz",
email: "fdfdfd@gmail.com"
}
),
// Adding headers to the request
headers: {
"X-Auth-Token": "api-key o9q5s264jbp9dws0nsevnagqdst81esh",
"Content-type": "application/json"
}
})
// Converting to JSON
.then(response => response.json())
// Displaying results to console
.then(json => console.log(json));
解决方案
正如azbarcea所说,这是一个经典的 CORS 问题。
至于您对为什么 cURL 有效但 Fetch API 无效的评论,您可以参考 Stack Overflow 中的这个答案。
推荐阅读
- wordpress - 如何使革命滑块背景图像大小响应屏幕大小
- git - SSL_ERROR_SYSCALL 和 gnutls_handshake() 失败 - SSL/HTTPS 失败 [Ubuntu Server 18.04]
- binary - 一阶和二阶导数边缘检测中的阈值
- ionic4 - 离子 CORS 错误,但服务器已启用 CORS
- python - 将 .isin 应用于 DF 中的多个系列
- ios - iOS 11.0 推送视图控制器与 navigationItem.searchController 的问题
- c# - 进行 Windows 身份验证时首先触发什么功能
- python - 我想在python中改变图像的颜色
- scala - 如何在 Spark 中使用 GraphFrames 向 Neo4j 图形添加关系
- wordpress - 仅获取循环中的帖子,其中 id 保存在其他自定义帖子元数据中