Javascript 从chrome扩展调用API时出现问题
我正在尝试构建一个chrome扩展来生成一个内容脚本,我正在尝试进行一个API调用,但是我无法得到响应。 我收到了CORS和状态200的回复。 我已经将外部API主机附加到manifest.json上的权限部分 我的代码是:Javascript 从chrome扩展调用API时出现问题,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在尝试构建一个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(
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"
}
]
}
错误:
未捕获(承诺中)语法错误:JSON输入意外结束
在getIpReport.js:23中,内容脚本无法再进行跨源请求,请在后台脚本中执行
感谢您wOxxOm当您将virustotal URL粘贴到地址栏时,是否看到有效的JSON响应?例如,如果我在没有API密钥的情况下尝试此操作,则响应为空。这将导致您描述的错误。内容脚本无法进行跨源请求,请在后台脚本中执行。Chris G,是的,它可以工作