简体   繁体   中英

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.

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!

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

You need to create a proxy server in Heroku in order to get the data. Cors is restricting us to call the data directly to our browser not server to server interaction. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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