1

I have a problem with ajax request to Steam. I want to get price from steam market.

function jPrice(httpToJson) {
    $.getJSON(httpToJson, function(data) {
        return data.median_price;
    });
}

When I call function

jPrice('http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29');

I get an error:

XMLHttpRequest cannot load http://steamcommunity.com/market/priceoverview/?country=US&currency=1&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://lоcalhоst:63342' is therefore not allowed access.

I try:

  • Set php header Access-Control-Allow-Origin to *
  • JSONP

RESULT -> The same thing (error)!

Maybe someone knows a solution to this problem?

cifiko83
  • 13
  • 2

1 Answers1

1

You won't be able to get the results in your browser via ajax request made directly against steamcommunity.com, neither by setting the header Access-Control-Allow-Origin to *, nor by sending a JSONP request.

For this to work, steamcommunity.com should either add CORS headers in the response (the error message you're seing means that they are not there), or format the output to be JSON-P. They didn't do either.

This is a browser restriction, do not allow the content from a different origin to be loaded via ajax. What you need to do is introduce a middle-ware, so have your back-end server to make a request against steamcommunity.com and return the same response, and make the ajax call against you're server. This will work, your back-end is sending the request, and as it is not a browser request, the response will land, than your ajax call will be able to get the response as well since it is issued against the same domain

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • Thank you for your answer! I try one more time to set CORS. I search php script [there](http://stackoverflow.com/a/18399709) When i reload server and browser on page derived "You have CORS!" But when i try to get ajax request => same error :( – cifiko83 Nov 18 '14 at 08:07
  • you welcome. The problem is that their server didn't configure CORS. What the post is describing is the way you would configure you server to support cross-origin requests. What you should do instead is make a PHP script that sends the request you were sending from ajax, and return the response. And than, from your ajax target your page – Master Slave Nov 18 '14 at 09:06