I'll preface this by saying my JavaScript-fu is not amazing.
That said, I'm trying to add a quick API call to a page on my local network. An Apache web server is accessible on the network via http://plex/. Flexget's API is at http://plex:5050/api.
I'm accessing a webpage through my browser (Chrome v57) that is attempting to access the Flexget API via XMLHttpRequest. I'm able to access the login endpoint without issue, and I've verified the cookie is now in my browser.
However, trying to then access the entry_list endpoint results in this in Chrome's JavaScript console:
OPTIONS http://plex:5050/api/entry_list/ 401 (UNAUTHORIZED)
Response for preflight has invalid HTTP status code 401
I also tried using an Authorization Token header, same result.
From doing some Google searches it sounds like an issue with CORS, maybe? My (extremely limited) understanding is that the preflight call doesn't send authorization information, but if the server understands the CORS protocol, it's supposed to basically return status 200 and say, "Yeah, that's a method that's allowed" and then the browser sends the full request with authorization info.
Does that sound right? What am I doing wrong? Has anybody else gotten this working this way? Obviously it's working somehow, or the web UI wouldn't work at all ...
var request = new XMLHttpRequest();
request.open('POST', 'http://plex:5050/api/auth/login/?remember=true');
request.setRequestHeader('Content-Type', 'application/json');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
var logged_in = 'yes';
}
};
var body = {
'username': 'flexget',
'password': 'uuqiyq-betty-boop'
};
request.send(JSON.stringify(body));
function loadSeriesAttribs(series_id) {
if (series_id != '') {
var request = new XMLHttpRequest();
request.open('GET', 'http://plex:5050/api/entry_list/'); //1/entries/' + series_id + '/');
request.setRequestHeader('Content-Type', 'application/json');
//request.setRequestHeader('Authorization', 'Token b3ae1ff22dc0885a6633d0f6ebe475ae4fd487751971f5969b7d6dc0')
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
var logged_in = 'yes';
}
};
request.send(JSON.stringify(body));
}
}