[英]How can I get JSON data from a browser extension (Chrome) from another server?
我正在编写一个浏览器扩展程序,它应该定期接收 JSON 格式的汇率并将其放在本地存储中。 但是当我尝试在任何网站的页面代码中执行此操作时,我会收到错误消息:
Access to XMLHttpRequest at 'https://my.server.domain/currency.txt' from origin 'https://developer.mozilla.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
示例请求代码
$updateUrl = "hhttps://my.server.domain/currency.txt";
$.get($updateUrl).done(function($updData) {
});
我可以运行这样的脚本的唯一地方是popup.html ,但我需要自动触发更新。 我试图通过背景做到这一点,但在清单 v3中不再有这种可能性。
唯一的选择是定期打开 update.html 扩展页面并在其中执行更新,但这是通过打开一个新选项卡来实现的,我想在后台执行此操作。
有小费吗?
一般来说,我在background.js中写了这个并且它有效
fetch("https://my.server.domain/currency.txt")
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
现在仍然要弄清楚如何使脚本定期更新数据,否则它只能工作一次。 不过那是另一回事了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.