Steam API Get SteamID using Javascript

我们两清 提交于 2019-12-01 20:26:28

问题


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.

For example, my username: "Emperor_Jordan" I would go to:

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. 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).

Am I approaching this the wrong way, is there a workaround?

Thanks!


回答1:


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. 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



来源:https://stackoverflow.com/questions/13150879/steam-api-get-steamid-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!