I have been facing a similar issue as described in , but unfortunately, the accepted solution did not work for me.
The problem I am encountering is with making an ajax request from a client script. The request works perfectly fine locally, but when running it on a test server, it fails.
Here is a snippet of my ClientScript.js:
var AppViewModel = function () {
var self = this;
self.Refresh = function () {
$.ajax({
url: "http://localhost/AppName/ControllerName/GetData",
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data, textStatus, jqXHR) {
//perform actions here
},
error: function (jqXHR, textStatus, errorThrown) {
//handle errors
},
complete: function (jqXHR, textStatus) {
//additional tasks
}
});
}
To address the CORS issue in my MVC web service, I have added the following function to global.asax.cs:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://testdev");
}
Despite implementing the above changes, the error persists while deploying to the "testdev" server:
XMLHttpRequest cannot load http://localhost/AppName/ControllerName/GetData.
Origin http://testdev is not allowed by Access-Control-Allow-Origin.
What could be missing or causing this issue?