plusheen plusheen - 1 year ago 95
reST (reStructuredText) Question

JQuery Ajax requests to Microsoft Office365 Unified API Failing with Error Code 0

I'm currently trying to authenticate with the Microsoft Unified API to retrieve data. I've followed their deep dive which goes through the connection in Fiddler, and managed success with it. I'm now trying to convert this into code to run on a test website.

I have to create a POST request to the specified URL, which I have done via the following code:

var url = "" + tenantId + "/oauth2/token";
var data = 'grant_type=authorization_code' +
'&redirect_uri=http://localhost:61104/Views/Home.html' +
'&client_id=' + clientId +
'&client_secret=' + clientSecret +
'&resource=' +
'&code=' + oauthCode;

type: "POST",
url: url,
headers: {
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
data: data,
success: function (a, b) {
error: function (a, b, c) {

However, this hits the error function, and the response gives me nothing except "error code 0". I've tried removing all data and headers and just posting to the URL, and again I get error code 0.

I've googled around and ensured that it's not my page refreshing or reloading, which it isn't. I tested this by posting to another REST service, which worked fine.

This is the first I've done with oAuth and posting so I assume i'm making a rookie mistake somewhere. What am I missing? Thanks.

Edit: To add, I tried a bare minimum request, with just:

type: "POST",
url: url

and it still fails with error 0. I replicated this in Fiddler, and I got back the expected response (Error 400).

Answer Source

I solved this by adding:

    <add name="Access-Control-Allow-Origin" value="*" />

to the web.config file. I also had to enable cross site scripting in IE settings.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download