Joshua
Joshua

Reputation: 4320

Trouble displaying JSON results of jquery GET

I am new to Javascript, JQuery and JSON, so I may be making many different mistakes, but I've always found the best way to learn was to jump right in.

I am working on a little pet project, mostly for practice, using an API provided on the site elophant.com. It's for tracking statistics from the popular League of Legends game. However, I cannot seem to get any jquery to work correctly. Here is the API of the resource I am trying to access: http://elophant.com/developers/docs/status.

Their example request is: "GET http://elophant.com/api/v1/status?key=< key >" The < key > field is a key they provide you once you register to the site. It returns information in JSON.

Here is what I am trying to do so far:

<body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

    <script>
    $.get("http://elophant.com/api/v1/status?key=MY_KEY_HERE", {callback : "?" }, "jsonp",  function(data) {
    $('#summary').text(data.result);
    });
    </script>
</body>

EDIT: I removed the JSONP and replaced it with plain JSON, and I receive this console error:

XMLHttpRequest cannot load http://elophant.com/api/v1/status?key=MY_KEY_HERE&callback=%3F. Origin null is not allowed by Access-Control-Allow-Origin.

Upvotes: 0

Views: 531

Answers (1)

Mike Brant
Mike Brant

Reputation: 71422

It seems that, at a minimum, you have your parameter order wrong. The dataType must be after the callback function like this:

$.get("http://elophant.com/api/v1/status?key=MY_KEY_HERE", {callback : "?" },  function(data) {
    $('#summary').text(data.result);
}, "jsonp");

I also don't see anything in that API documentation that indicates it supports wrapping of the JSON in a JSONP callback function. Are you sure this is available?

You can test this by verifying that the response you get is JSON wrapped in a function. That might look something like this:

callback({key: "value", anotherKey: "another value"})

Upvotes: 1

Related Questions