Today I found a strange behavior of XMLHttpRequest. When I am calling a GET service I found that if I do not set the Authorization header the request from firefox is same. But if I add the "Authorization" header firefox first send a request with "OPTIONS" then it sends a "GET" request.
I know that the verb "OPTIONS" must be handled in server side but I was just wondering why XMLHttpRequest behaves like this. Though it is a cross domain request, why browser first send the "OPTIONS" request. Why adding a "Authorization" header changes the behavior.
Here is my Javascript code and Fidler Inspector report.
var xmlhttp = new XMLHttpRequest();
var url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
xmlhttp.open('GET',url,true);
xmlhttp.setRequestHeader("Authorization", "xxxxxxxxxxxxxxxxxxx");
xmlhttp.send(null);
xmlhttp.onreadystatechange = function() {
alert("OnReadystatechange + " + xmlhttp.readyState + " " + xmlhttp.status);
if (xmlhttp.readyState == 4) {
if ( xmlhttp.status == 200) {
}
else {
}
}
else
alert("Error ->" + xmlhttp.responseText);
}
And the fiddler response with Authorization Header
But when I do not add the Authorization header the browser directly sends the GET request no OPTIONS request.