I'm trying to get an authentication token from microsoft. First I send a get request and get a code in response, which I write to a var called auth_code. Then I use that returned auth_code and some other vars like this:
$.ajax({
type: "POST",
url: url + tenant + '/oauth2/v2.0/token',
data: {
client_id: client_id,
client_secret: client_secret,
redirect_uri: redirect_uri,
scope: scope,
code: auth_code,
grant_type: 'authorization_code'
},
success: function(data){
alert("success");
},
error: function(xhr) {
alert(xhr.statusText);
}
});
Error is triggered every time; I'm getting statusText of 'error'.
If I breakpoint at the error alert, in the firefox debugger under network I see a 200 for the token url, and in the response I see the json returned by microsoft (including a lovely auth token), i.e. response is not null. But the ajax success handler doesn't run, so I can't use it.
Things I've read and tried:
I've copied and pasted the json response from the debugger into json lint and it validates ok (in any case, I believe I would have got an error textStatus of 'parseerror' if that was the problem?).
I've tried explicitly adding a dataType of 'json', 'text' and 'text json' but it makes no difference.
I just don't get it - the response is ok, the return json is there in the debugger, so why is the ajax failing to handle it (effectively preventing me from using it)?
It's Friday, so hopefully I'm just missing something really simple and obvious - any help appreciated.