2016-03-08 148 views
0

網絡編程有點新奇,並且有點困惑。我有一個基本的快速node.js web服務器提供一個網站。我想,隨手一個遊戲ID的功能,並將它使用應使用以下REST API調用來支持他們的Web API抓住從蒸汽成績信息:Steam API訪問控制 - 允許來源問題

https://developer.valvesoftware.com/wiki/Steam_Web_API#GetGlobalAchievementPercentagesForApp_.28v0002.29

我有一個腳本文件正在擔任了我試圖在像這樣的客戶端文件中做請求。

function GetSteamGameAchievements(appid) 
{ 
    var request = new XMLHttpRequest(); 

    request.addEventListener('load',function(){ 

     console.log(JSON.stringify(this.responseText)); 

    }); 

    request.open("GET","http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid="+ appid + "&format=json",true); 

    request.send(null); 
} 

我收到以下錯誤

的XMLHttpRequest無法加載 http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=440&format=json。 請求的 資源上沒有「Access-Control-Allow-Origin」標題。 'http://localhost:3000'因此不允許 訪問。

我已經讀了一些關於錯誤的信息,但似乎需要更改服務器端代碼或其他東西。雖然所有的服務器端代碼不喜歡的東西:

// Add headers 
app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://api.steampowered.com'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 

使用Open API天氣這是唯一的其他第三方REST API,我的東西,如原代碼的工作就可以了,所以我不確定爲什麼Steam API無法正常工作。使用郵遞員我提交的查詢字符串沒有問題,但是當我在客戶端JavaScript中執行此操作時,證券出現問題...但未打開天氣API ...我不確定它爲何適用於但不是其他。任何幫助將非常感激,我已經走過了一些類似的線程,但我認爲我的無知使我很難理解我哪裏出錯,哪裏需要修正。

回答

2

我讀過一些有關的錯誤,但它似乎需要服務器端代碼更改或東西。

是 - 由您要讀取數據的服務。即通過蒸汽。

您不能允許自己使用訪問者的瀏覽器從其他人的網站讀取數據。

由於您無法更改Steam的服務器,因此您需要使用服務器而不是訪問者的瀏覽器從Steam獲取數據。

+0

所以只需要做一個獲取請求到我的服務器,並重新調整請求的請求模塊來調用蒸汽API然後迴應我得到我的服務器的任何響應,然後將其發送到客戶端? – user2927848

+0

@ user2927848 - 是的。 – Quentin

-2

你可以很容易'cors'模塊。 使用此命令

npm install cors --save. 

,並在您的app.js添加這些行

var cors = require('cors');  
app.use(cors()); 

你並不需要得到你的代碼dirty.This是一個更清潔的方式。

刪除此

app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://api.steampowered.com'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 
+0

我已經包括它,我仍然得到錯誤,我必須以不同的方式格式化我的網址? – user2927848

+0

你如何在app.js中定義你的路線? –