This question has been asked a few times and answered as well, however I am trying to implement the suggested approach but going nowhere. Its a typical CORS issue. Except in my case I want to be able to use localhost or my pc IP on the browser. If my config has been set to use IP, using localhost gives me the CORS error and vice versa. Any guidance is appreciated. Note: Please ignore different ip addresses in my screenshot and text.
Failed to load resource: the server responded with a status of 405 (Method Not Allowed) localhost/:1 XMLHttpRequest cannot load http://192.168.1.2/ARES/store/1.0/core/rest/authenticate/-99/-99. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405.
My code is as below: I cant find a way to set the header correctly to us Allow-Origin.
Ext.Ajax.request({
url: url,
timeout: 2000,
cors: true,
method: 'GET',
headers: {
'Access-Control-Allow-Origin': 'http://localhost/'
},
success: function(response, options) {
Ext.getBody().unmask();
Manager.app.fireEvent('showLoginView');
},
failure: function(response, options) {
Ext.getBody().unmask();
}
});
When I run it in the browser I see Request Headers as below:
It is done on the webservice end: This is how I achieved it: Following code goes in the Global.asax file. Hope it helps someone.
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS,PUT, DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}