Having problems with an Ajax call to get an object of members in a team

I am working on something for my own website to display a list of members of a team that someone is in.
The problem I am having is getting an array of members for a Team.
So far this is what my code looks like (sorry if it looks a bit messy)

var ar = [];
var names = [];
var team = 'buildguild';
$.ajax({
    url: 'https://api.twitch.tv/api/team/'+team+'/all_channels.json',
    type: 'GET',
    dataType: 'json',
    headers: {
        'Access-Control-Allow-Orgin':'*'
    },
    success: function(data) {
        console.log(data);
        for (var i = 0; i < data.channels.length; i++) {
            names.push(data.channels[i].channel.display_name);
            ar[data.channels[i].channel.display_name] = data.channels[i].channel;
            $('.members').append('<div><span style="margin-left:5px;">Name: '+data.channels[i].channel.display_name+'</span><br><span style="margin-left:5px;"> Viewers: '+data.channels[i].channel.current_viewers+'</span><br><span style="margin-left:5px;"> Status: '+data.channels[i].channel.status+'</span></div>');
        }
        console.log(ar);
        console.log(names);
    },
    error: function(err) {
        console.log(err)
    }
})

When I try to get the array of members in the Team I get this error:

XMLHttpRequest cannot load https://api.twitch.tv/api/team/buildguild/all_channels.json. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access.

I’ve searched for anything that could possibly help me but nothing that I have found has for me worked.
I’ve also tried to do this call via XMLHttp request and it still didn’t work.
If someone could help me that would be amazing.

/api/ URLs are not part of the documented API and it seems like they don’t support CORS. You might be able to request it using jsonp, otherwise you’ll need a server to proxy the requests.

JSONP is no longer supported either.

Alright well thanks for the help.

You are also better off, running a server side script say once per 5 minutes instead and that saves abusing the Twitch API to hell with a huge stack of requests every page load by every user.

Seems like it has plenty of documentation, and supports JSONP

Perhaps the header fed in is not correct? Or that they changed endpoint versions?

The docs say to use:

Accept: application/vnd.twitchtv.v2+json

the v2 or v3 is dealers choice on which function endpoint version you want.

cheers.

api.twitch.tv/ kraken / is not the same as api.twitch.tv/ api /

The former is designed for 3rd party consumption and documented in the link you posted, the latter is not.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.