I am getting an error in Chrome when calling a webservice on my own box. The error is:
Origin http://localhost:52386 is not allowed by Access-Control-Allow-Origin.
I have read other questions here and articles like this: Cross Domain AJAX; but they tend to deal with the problem of IE not working correctly and I have a problem with Chrome.
I added the following to my webservice/Global.asax.cs:
protected void Application_BeginRequest(object sender, EventArgs e)
{
//HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
// Changed to this:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:52386");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:80");
}
Here is my HTML code (I need to keep this in plain HTML, no code behind):
$(document).ready(function () {
var user = 'testuser';
var pwd = 'abc123';
var auth = 'Basic ' + Base64.encode(user + ':' + pwd);
if ($.browser.msie) {
jQuery.support.cors = true;
$.ajax({
type: "GET",
url: "http://localhost/Website.Webservice/api/TaxDocument",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "text json",
headers: { Authorization: auth },
crossDomain: true,
success: function (data, textStatus, jqXHR) { ProcessOutput(data); },
error: function (jqXHR, textStatus, errorThrown) { alert('Error: ' + textStatus + ' ' + errorThrown); }
});
} else {
$.ajax({
type: "GET",
url: "http://localhost/Website.Webservice/api/TaxDocument",
data: "{}",
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
headers: { Authorization: auth },
success: function (data, textStatus, jqXHR) { ProcessOutput(data); },
error: function (jqXHR, textStatus, errorThrown) { alert('Error: ' + textStatus + ' ' + errorThrown); }
});
}
})