3

I am trying to call a webservice which is hosted on my localhost. This AJAX call works for me and I can see the "Authorization" token being sent in the header of the request to the service.

    $.ajax({
        type: method,
        url: serviceUrl,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Authorization",  Token); },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        ProcessData: true,
        cache: false,
        success: successHandler,
        error: errorHandler
    });

But this time I try to call the service which is hosted on Azure Cloud and the same AJAX call works but it does not send the header content.I am using fiddler to inspect the header content of the requests. Is there something I can do so that the header has the "Authorization" token?

Abhishek J
  • 193
  • 2
  • 14

2 Answers2

2

It could possibly be a cross-domain policy restriction. You have to make sure the ajax call and the service are within the same domain or you will have to do a little hacking to get it working.

Frank Natividad
  • 604
  • 6
  • 17
  • I hosted application(from which I make the ajax call) and the WebService on Azure cloud and both are on the same domain. But still I do not see the token being sent in the header of the ajax request.Is there something that I can do so that I can find the root cause? – Abhishek J Nov 21 '12 at 18:27
0

There are security policies at the browser level to prevent cross-domain AJAX calls. There are multiple workarounds for this (such as using XML instead of JSON or this jQuery plugin), but the most "correct" seems to be using JSONP instead of JSON, which I think you can do just by changing the dataType in your AJAX call:

$.ajax({
    //...etc.
    dataType: "jsonp",
    //...etc.
});

If that's not the problem, try adding return true; to your beforeSend handler. The request gets canceled if that returns false, but I don't know what happens if it doesn't return a value at all.

Community
  • 1
  • 1
Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • I changed the dataType:"jsonp" but even this does not seem to fix the issue. I still do not see any data being sent in the header of the AJAX request. – Abhishek J Nov 21 '12 at 18:41
  • I tried that but still it doesn't work.Is there some other issue which is not allowing the ajax call to send the token in the header? – Abhishek J Nov 22 '12 at 05:14