简体   繁体   English

Steam API 使用 Javascript 获取 SteamID

[英]Steam API Get SteamID using Javascript

Been running into what appears to be the Same Origin Policy which is causing quite some headache!遇到了似乎是同源策略的问题,这引起了相当大的头痛!

To cut to the chase, I am essentially trying to acquire a user's steam64id when only supplied their username.为了切入正题,我实际上是在仅提供用户名时尝试获取用户的 steam64id。

For example, my username: "Emperor_Jordan" I would go to:例如,我的用户名:“Emperor_Jordan”我会去:

http://steamcommunity.com/id/emperor_jordan?xml=1 http://steamcommunity.com/id/emperor_jordan?xml=1

And the steamid I need is right at the top.我需要的蒸汽就在顶部。 So I figured I would use JQuery Ajax to acquire this and parse out the id I need for later usage (steamapi usage requires the steam64id) as follows.所以我想我会使用 JQuery Ajax 来获取它并解析出我以后使用所需的 id(steamapi 使用需要 steam64id),如下所示。 Here is a snippet of the code in question:这是有问题的代码片段:

$.ajax({
url: "http://steamcommunity.com/id/emperor_jordan/?xml=1",
datatype: "xml",
complete: function()
{
    alert(this.url)
},
success: parse
});

function parse(xml)
{
    alert("parsing");
    _steamID = $(xml).find("steamID64").text();
}

The problem here is while I do get the alert for the completion, I never see "parsing".这里的问题是当我收到完成警报时,我从未看到“解析”。 Ever.曾经。 It never gets that callback, which leads me to believe I am running into the SOP (same origin policy).它永远不会得到那个回调,这让我相信我遇到了 SOP(同源策略)。

Am I approaching this the wrong way, is there a workaround?我是否以错误的方式接近这个问题,有解决方法吗?

Thanks!谢谢!

Correct.正确的。 You are running into the same-origin policy:正在运行到同源策略:

XMLHttpRequest cannot load http://steamcommunity.com/id/emperor_jordan/?xml=1. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

and it looks like Steam does not offer a cross-origin solution like JSONP .看起来 Steam 没有提供像 JSONP 这样的跨域解决方案 That means you're back to the old-but-reliable solution: fetch the data on your server, not in the browser.这意味着您又回到了旧但可靠的解决方案:在您的服务器上获取数据,而不是在浏览器中。


Some relevant feedback on the Steam Web API: https://developer.valvesoftware.com/wiki/Steam_Web_API/Feedback#API_Considerations_for_Web_Developers Steam Web API 的一些相关反馈: https : //developer.valvesoftware.com/wiki/Steam_Web_API/Feedback#API_Considerations_for_Web_Developers

You need to create a proxy server in Heroku in order to get the data.您需要在 Heroku 中创建一个代理服务器才能获取数据。 Cors is restricting us to call the data directly to our browser not server to server interaction. Cors 限制我们将数据直接调用到我们的浏览器,而不是服务器到服务器的交互。 So we need a proxy server to send the requests and receive the data on our behalf.所以我们需要一个代理服务器来代表我们发送请求和接收数据。 It's working for me.它对我有用。

Thanks in advance.提前致谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM