javascript - 从 chrome 扩展调用 API 的问题
问题描述
我正在尝试构建一个 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);
})
清单.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"
}
]
}
错误:未捕获(承诺中) SyntaxError:getIpReport.js:23 处 JSON 输入意外结束
解决方案
内容脚本不能再发出跨域请求,请在后台脚本中进行,谢谢 wOxxOm
推荐阅读
- javascript - 一个反应组件出现而不是预期的
- php - Mac OS - 未找到 PHP 致命错误类“Mongo”
- javascript - 为 HackerRank QHEAP1 优化 node.js 解决方案
- java - 与 jsf spring 和 xray 集成
- python - 获取当前到第n个深度的目录
- r - 使用 Rvest 从网页中提取数据不断获取 NA、"" 或 {xml_nodeset (0)}
- email - MS Graph API - 从 Outlook 帐户读取带有标题信息的电子邮件的端点
- python - 使用python在外部数据库文件中插入和更新+1行
- latex - 我可以将选项卡、summary() 表从 Stata 导出到 LaTeX 吗?
- python-3.x - 我手动创建的 power() 函数显示 TypeError