我正在尝试构建一个 chrome 扩展来制作内容脚本,我正在尝试进行 API 调用,但我无法得到响应。 我收到类型为 CORS 且状态为 200 的响应。 我已经将外部 API 主机附加到了 manifest.json 的权限部分
我的代码是:
fetch('http://www.virustotal.com/vtapi/v2/ip-address/report?ip=8.8.8.8&apikey=My API KEY')
.then(res => {
console.log(res);
// Response{type: cors, url:http://www.virustotal.com/....... status: 200}
return res.json(); // Here is the problem
})
.then(data => {
console.log(data);
})
manifest.json:
{
"name": "IP information on hover",
"description": "IP information on hover",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_icon": "hello_extensions.png"
},
"permissions": [
"https://www.virustotal.com/",
"http://www.virustotal.com/"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"jquery-3.4.0.js",
"getIpReport.js"
],
"run_at": "document_end"
}
]
}
错误: 未捕获( promise 中)SyntaxError:JSON 输入意外结束 在 getIpReport.js:23
最佳答案
内容脚本无法再发出跨源请求,请在后台脚本中执行 谢谢wOxxOm
关于javascript - 从 chrome 扩展调用 API 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904548/