0

I'm pretty new to ajax and I faced issue, that is not solvable by any of available stackoverflow's threads. I've got array with streams names and I use ajax to get detailed info about them by using Twitch API. For some of those requests I get following error:

Error: callback was not called
    at Function.error (VM11084 jquery.min.js:2)
    at b.converters.script json (VM11084 jquery.min.js:4)
    at Nb (VM11084 jquery.min.js:4)
    at A (VM11084 jquery.min.js:4)
    at HTMLScriptElement.c (VM11084 jquery.min.js:4)
    at HTMLScriptElement.dispatch (VM11084 jquery.min.js:3)
    at HTMLScriptElement.q.handle (VM11084 jquery.min.js:3)

I found it peculiar that generated url (check url variable in code below) is ok and when I paste it in chrome's search bar, it leads to proper result. The most confusing part is that it works properly for 4 strings in streamsNames array and for others it does return this error code. I googled a lot however I cannot find proper solution or just explanation of this behaviour.

Here's the relevant part of the code:

function updateStreamInfo() {
  streamsNames.forEach(function getStreamsCurrentData(streamName) {
    var url = 'https://wind-bow.glitch.me/twitch-api/streams/' + streamName;
    console.log(url);
    $.ajax({
      url: url,
      dataType: 'JSONP',
      jsonpCallback: 'callback',
      type: 'GET',
      success: function (data) {
        console.log(data);
        var stream;
        if(data.stream === null) stream = new Stream(streamName, "", "");
        else stream = new Stream(streamName, data.stream.game, data.stream.channel.logo);
      },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(thrownError);
      }
    })
  })}

And one more thing: this whole things runs in codepen.io but I do not know if that has any influence on anything in this case.

Any ideas what might be the issue?

kkotula
  • 147
  • 1
  • 11
  • http://stackoverflow.com/questions/6215235/jsonpcallback-function-not-working – Matt Mar 27 '17 at 22:31
  • Which version of jQuery are you using? – Explosion Pills Mar 27 '17 at 22:36
  • JSONP (as explained [in the answer to this question](http://stackoverflow.com/q/3839966/215552)) requires that the callback is called with the data returned by the service. – Heretic Monkey Mar 27 '17 at 23:07
  • ExplosionPills I use jQuery 3.1.1 MikeMcCaughan and mkaatman I removed _jsonpCallback:_ _'callback'_ and now it's working for all values in array. Thank you very much. However I still don't get why it was working for half of the array, and for other half it was failing. If it was failing for each element it would be obvious somethings wrong with callback function or connection, but in this case it was partially working. Anyway I'm grateful for the help! – kkotula Mar 28 '17 at 17:28

0 Answers0