我使用 chrome.identity.getAuthToken
与某些内部 API 进行通信。
前 20 小时一切都很完美,我可以调用 API 并获得我需要的东西。
大约 20 小时后,我的扩展停止工作,我得到:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MY_API' is therefore not allowed access.
我知道 chrome.identity.getAuthToken
将更新过期的 token ,并且我可以在 chrome://identity-internals/
看到我的代码获得了新的访问权限 token 但无法发送请求。
如果我再次卸载/安装扩展程序,它就会再次工作。
list 包含:
"permissions": [ "tabs", "activeTab", "cookies", "identity", "<all_urls>" ]
当我重新安装扩展程序时会发生什么情况,从而使错误消失?
背景.js:
chrome.identity.getAuthToken({interactive: true}, function(token) {
gapi.auth.setToken({'access_token': token});
// Load the storage API.
gapi.client.load(API_ROOT + DISCOVERY_PATH,
VERSION, function() {
// Initialize GAPI's OAuth.
gapi.auth.init(function() {
var request = "It's request to my API";
// Not working after 20 hours
request.execute(function(response) {
// do something
});
});
});
})
还尝试调用“chrome.identity.removeCachedAuthToken”,但扩展程序在 20 小时内停止工作。
最佳答案
在服务器“https://MY_API”上设置此设置:
Header always append X-Frame-Options SAMEORIGIN
Header add Access-Control-Allow-Origin "*"
在 .htaccess 或 httpd.conf 中(如果服务器是 httpd/apache)
关于javascript - Chrome 扩展程序 20 小时后没有 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129633/