2017-05-02 25 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 –