-7

I am trying to make a cross-domain AJAX call with the latest jQuery for the Twitch.TV API and I'm getting an error.

Code:

 $.ajax({
    type: 'GET',
    dataType: "json",
    url: "https://api.twitch.tv/kraken/search/games?q=star&type=suggest",
    success: function (responseData, textStatus, jqXHR) {
        console.log("in");
        var data = JSON.parse(responseData['AuthenticateUserResult']);
        console.log(data);
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

Error:

XMLHttpRequest cannot load https://api.twitch.tv/kraken/search/games?q=star&type=suggest. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://codeeplus.net' is therefore not allowed access.

Anthony
  • 36,459
  • 25
  • 97
  • 163
user2812028
  • 265
  • 1
  • 6
  • 17

1 Answers1

1

You need JSONP for cross-browser requests. The link you gave me works fine with getJSON jquery function.

for streams: http://jsfiddle.net/82wNq/27/

for games: http://jsfiddle.net/82wNq/25/

$.getJSON("https://api.twitch.tv/kraken/search/games?q=star&type=suggest&callback=?", function (data) {
    $.each(data.games, function (index, item) {
        $("<div>").html(item.name).appendTo("#content");
        $("<img>").attr("src", item.box.medium).appendTo("#content");
    });
});
thenewseattle
  • 1,451
  • 1
  • 13
  • 17
  • This worked! But how would I do this with /search/streams instead of /search/games ? I'm guessing you need to change data.games to something else, I tried with just data and that didn't work. Any ideas? @thenewseattle – user2812028 Nov 22 '13 at 23:17