【发布时间】:2020-06-05 19:36:41
【问题描述】:
我尝试从我的后台脚本中获取数据,如推荐的 here,但它总是被阻止。我错过了什么吗?
Background.js:
chrome.storage.sync.get(['sigurl'], function(result) {
console.log(result.sigurl);
fetch(result.sigurl)
.then((response) => {
return response.text();
})
.then((html) => {
console.log(html);
chrome.storage.sync.set({'sig': html}, function() {});
})
.catch(function(err) {
console.log('Failed to fetch page: ', err);
});
});
控制台:
Access to fetch at 'http://example.com/test.html' from origin 'chrome-extension://pbflkjmmkpgddamdcihlbggdccjmjmbk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
使用 no-cors 模式也不起作用。
【问题讨论】:
-
将
http://example.com/添加到permissions in manifest.json。 -
@wOxxOm 后台脚本不应该被CORS阻止吗?
-
@avalanche1,请参阅我在第一条评论中提供的链接。