Javascript 从docker主机获取JSON API
我从Dockertools box构建qBittorrent。qBittorrent有一个API获取torrentlist。就是这样,Javascript 从docker主机获取JSON API,javascript,json,web,Javascript,Json,Web,我从Dockertools box构建qBittorrent。qBittorrent有一个API获取torrentlist。就是这样, GET /query/torrents HTTP/1.1 User-Agent: Fiddler Host: 127.0.0.1 Cookie: SID=your_sid 但是我在这个主机上运行它:所以我使用javascript的loadJSON()来捕获我想要的内容。但它总是失败。 在控制台中执行以下操作: XMLHttpRequest无法加载http://
GET /query/torrents HTTP/1.1
User-Agent: Fiddler
Host: 127.0.0.1
Cookie: SID=your_sid
但是我在这个主机上运行它:所以我使用javascript的loadJSON()来捕获我想要的内容。但它总是失败。
在控制台中执行以下操作:
XMLHttpRequest无法加载http://192.168.99.100:8080/query/torrents?filter=uploading&appid=f782f1c06749ea791dcb5d22219adf068a0d16a5a2c7b6686b17459562eb6b4f. 请求的资源上不存在“Access Control Allow Origin”标头。因此,不允许访问源站“null”。
我的代码在这里:
var torrent_peer;
function loadJSON(url, callback) {
var xobj = new XMLHttpRequest();
if (xobj) {
xobj.overrideMimeType("application/json");
xobj.open('GET', url, true); // Replace 'my_data' with the path to your file
xobj.withCredentials = true;
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send();
}
}
function load() {
loadJSON("http://192.168.99.100:8080/query/torrents?filter=uploading&appid=f782f1c06749ea791dcb5d22219adf068a0d16a5a2c7b6686b17459562eb6b4f", function(response) {
var actual_JSON = JSON.parse(response);
console.log(actual_JSON);
}, 'jsonp');
}
我读了一些问题,但不起作用。所以任何人都可以帮助我。非常感谢 这是因为API没有指定允许您在允许的源之外访问它的头。这可能是Chrome的安全保护。试着跑步
chrome——禁用网络安全
但要小心,启用此功能后,您很容易受到网站攻击
另一种解决方法是将头添加到应用程序的API中。我不确定您的应用程序是否允许添加它
访问控制允许源代码:
如何运行此chrome--禁用web安全