2017-05-02 22 views
0

我从Dockertools框中构建qBittorrent。而qBittorrent有一个API 获得洪流列表。这就像:获取JSON API表单码头主机

GET /query/torrents HTTP/1.1 
User-Agent: Fiddler 
Host: 127.0.0.1 
Cookie: SID=your_sid 

但我在这台主机上运行它:http://192.168.99.100:8080/,所以我使用JavaScript的loadJSON()来捕捉我想要什么。但它总是失败。 这控制台:

XMLHttpRequest cannot load http://192.168.99.100:8080/query/torrents?filter=uploading&appid=f782f1c06749ea791dcb5d22219adf068a0d16a5a2c7b6686b17459562eb6b4f. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我的代码在这里:

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'); 
} 

我读了一些问题,但它不工作。所以任何人都可以帮助我。非常感谢 !

回答

0

这是因为API没有指定允许您在允许的原点之外访问它的标头。这可能是Chrome的安全保护。尝试运行

chrome --disable-web-security

但要小心,通过启用此功能,您很容易受到网站的攻击。

解决此问题的另一种方法是将头添加到应用程序的API中。我不确定你的应用程序将允许添加它。

Access-Control-Allow-Origin: *

+0

如何运行此chrome --disable-web-security –