Steam API Get SteamID using Javascript

Jordan Johns picture Jordan Johns · Oct 31, 2012 · Viewed 7.5k times · Source

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!

Answer

Matt Ball picture Matt Ball · Oct 31, 2012

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