0

Using the DotA 2 Web API, I'm trying to get the JSON from a certain URL: https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id=27110133&key=3C2DCF9BA05F56704FDF3F45BE2C6AD3. I would then like to use the JSON object like this json.players[0].kills.

I open the below html in a file using Chrome, and it gives me the error Uncaught SyntaxError: Unexpected token :.

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
      $.ajax({
        type: 'GET',
        url: 'https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id=27110133&key=3C2DCF9BA05F56704FDF3F45BE2C6AD3',
        async: false,
        dataType: 'jsonp',
        success: function(data)
        {
          console.log(data);
        },
        error: function(e)
        {
           alert(e.message);
        }
      });
    </script>
  </head>
</html>

I have used JSON before on iOS, but I am a total noob to web development, so apologies for what is a probably really simple question.

michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • try changing `dataType: 'jsonp'` to `dataType: 'json'` – Cerlin Dec 22 '14 at 04:32
  • @CerlinBoss I get `XMLHttpRequest cannot load https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id=27110133&key=3C2DCF9BA05F56704FDF3F45BE2C6AD3. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.` I followed [this answer](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource), and that's why I used 'jsonp'. – michaelsnowden Dec 22 '14 at 04:33
  • 1
    On browsing your URL it gives `Connection was reset error`, which is not a valid `json` so, it gives `Unexpected token error`. – Rohan Kumar Dec 22 '14 at 04:34
  • @RohanKumar Where did you see `Connection was reset error`? What does that mean I need to do? – michaelsnowden Dec 22 '14 at 04:35
  • @RohanKumar When I entered the URL, I am getting valid JSON, verified the JSON from Jsonlint.com as well. – Pramod Karandikar Dec 22 '14 at 04:36
  • 1
    This api doesn't seem to support jsonp or cors, a client side solutions doesn't seem possible. – Musa Dec 22 '14 at 04:37
  • @Musa Does that mean I can't use the API? – michaelsnowden Dec 22 '14 at 04:39
  • 1
    You'll have to use some server side script. – Musa Dec 22 '14 at 04:40
  • On opening the url given in `ajax` https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?match_id=27110133&key=3C2DCF9BA05F56704FDF3F45BE2C6AD3 – Rohan Kumar Dec 22 '14 at 04:48
  • anyone else getting "Uncaught SyntaxError: Unexpected token :" from the API? – ThisClark Dec 22 '14 at 04:50
  • @ThisClark Oh my god I'm so dumb! http://stackoverflow.com/questions/22716996/uncaught-syntaxerror-while-calling-json-from-dota-2-match-history-api – michaelsnowden Dec 22 '14 at 04:52
  • good find - looks like you're on the right track – ThisClark Dec 22 '14 at 04:54
  • possible duplicate of [Cross domain jquery ajax (Jsonp): Uncaught SyntaxError: Unexpected token : (colon)](http://stackoverflow.com/questions/21701750/cross-domain-jquery-ajax-jsonp-uncaught-syntaxerror-unexpected-token-colo) – michaelsnowden Dec 22 '14 at 04:54
  • you want this to be strictly client-side though, right? proxy server is out of the question i assume... – ThisClark Dec 22 '14 at 05:00
  • @ThisClark I'm hoping I can get this http://stackoverflow.com/questions/21701750/cross-domain-jquery-ajax-jsonp-uncaught-syntaxerror-unexpected-token-colo/21704177#21704177 to work, but I have no experience with web development, just iOS, so it might be difficult. Is there a client-side solution? – michaelsnowden Dec 22 '14 at 05:50
  • There's not a client side javascript solution, but the same code that would be used to setup a proxy should also be able to be used in the client on a mobile app, or a console based or gui application. I've done it with proxy web server in glassfish with java, and i've done it in a mobile app with android. I've never done anything in iOS but it stands to reason that if it works in the others, then it will work in iOS. Where do you ultimately want to use this content of yours? In a mobile webview using local files + ajax? In a website with client side script? – ThisClark Dec 22 '14 at 06:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67499/discussion-between-doctordoder-and-thisclark). – michaelsnowden Dec 22 '14 at 20:15

0 Answers0