Why does steam api refuse calls angular?

6

I'm simply trying to feed my application with Steam api data. Let's take this example: http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=440&format=xml

Paste into the browser and will work as intended.

But when I do in AngularJS :

function getData(){
    return $http.get('aquela url')
            .then(function(response){
                return response.data;
                })
            .catch(function(err){
                console.log(err);      
            });
}

It always falls into catch, with statuscode:0 as if the server was actively refusing my request.

What am I doing wrong?

    
asked by anonymous 31.05.2015 / 23:52

2 answers

2

To solve this problem you have to use a server as "proxy".

That is: For this request to work, it has to be done from a server and not from a browser. I give an example of PHP because it is the easiest, however this example is good for you to understand what to do. ps: I do not write php anymore

if (!empty($_GET['steamCall'])) {
    echo file_get_contents('http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=440&format=xml');
}

Then, in your controller instead of directly calling the steam API, you call the path of your server with ?steamCall=true . Your server goes to the steam API url and returns to your application what steamAPI answered the call from your server.

    
04.08.2015 / 17:01
1

You should have a problem with cors ( link ).

In a well-explained explanation, when you try to access something with ajax outside the source domain (as is the case with your site accessing steam), it does a previous request with the address pro method "asking" if your application can make the actual request.

I do not know the steam api, but they should not have enabled the cors. Since cors is a browser feature (implemented in the client browser), requests from your server are not affected, ie if you make a request with any programming language starting from your server, the cors is ignored.

Making a direct request via angular will not scroll unless the steam enables the cors. What can be done in this case is to use something on your server to make an exchange between the angle and the api that you want to access.

There are proxies for cors made in various languages. I do not know which one you are using, but if you search the google cors proxy you will find implementations in several languages.

    
06.05.2016 / 05:19