2017-01-03 39 views
0

在我宣佈我的問題之前,是的,我知道有關於此問題的其他線程,但不知道那麼或不能使用它們在我的項目。沒有「訪問控制允許來源」標題存在於蒸汽庫存的請求資源

所以現在到了這個問題。當我運行我的代碼時,如何在標題中看到標題,我得到的答案不是「Access-Control-Allow-Origin」標題。所以也許任何人都可以幫我找到這個錯誤。

代碼:

function GetInv(){ 
    var json_obj = JSON.parse(Get('http://steamcommunity.com/inventory/76561198122209518/730/2?l=english&count=5000')); 
    var assets = json_obj.assets; 
    console.log(json_obj); 
} 

function Get(yourUrl){ 
    var Httpreq = new XMLHttpRequest(); // a new request 
    Httpreq.open("GET",yourUrl,false); 
    Httpreq.send(null); 
    return Httpreq.responseText; 
} 

編輯

謝謝你的答案所以我用Google搜索非CORS兼容的工具,我發現這一點:http://www.test-cors.org/它給了我這個代碼片段:

var createCORSRequest = function(method, url) { 
var xhr = new XMLHttpRequest(); 
if ("withCredentials" in xhr) { 
// Most browsers. 
xhr.open(method, url, true); 
} else if (typeof XDomainRequest != "undefined") { 
// IE8 & IE9 
xhr = new XDomainRequest(); 
xhr.open(method, url); 
} else { 
// CORS not supported. 
xhr = null; 
} 
return xhr; 
}; 

var url = 'http://api.steampowered.com/ISteamEconomy/GetAssetClassInfo/v0001/?key=XXXXXXXXXXXXX&appid=730&class_count=1&classid0=1131459905'; 
var method = 'GET'; 
var xhr = createCORSRequest(method, url); 

xhr.onload = function() { 
console.log("super"); 
}; 

xhr.onerror = function() { 
console.log("failed"); 
}; 

xhr.send(); 

我試過了,但仍然是同樣的錯。所以,我要正確的方式,還是我愚蠢的找到答案?

+0

可能有[jQuery xml錯誤'No'Access-Control-Allow-Origin'標頭存在請求的資源。'](http://stackoverflow.com/questions/19821753/jquery-xml-error-no-access-control-allow-origin-header-is-present-on-the-req) – MayorMonty

回答

0

就在不久前,有辦法來欺騙人們在互聯網上,在網頁會假裝自己是用戶提交一個請求到另一臺服務器,讓他們妥協了用戶的帳戶。谷歌XSSCross Site Request Forgery欲瞭解更多信息。


由於上述安全問題,在現代瀏覽器,你不能讓另一個產地(域)的頁面的請求,除非他們明確地允許它與Access-Control-Allow-Origin頭,它是由域。沒有辦法解決這個問題,但我建議你使用Steam API實際上讓數據無法運行

+0

有一種解決這個問題的方法 - 使用非CORS兼容的工具,例如在服務器/ CLI上發出請求(如從命令行或Node.JS發出請求),瀏覽器實現CORS ... ,你仍應該像建議的那樣使用Steam API ......任何足夠提供API的人都應該尊重並用作獲取信息的主要來源。 – SamMorrowDrums

相關問題