Click here to Skip to main content
15,889,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting below error msg while calling Web API service from jquery.

XMLHttpRequest cannot load
http://localhost:7532/api/Common/GenerateTabs?clickedTab=null&null&_=1407394637890.
Request header field Access-Control-Allow-origin is not allowed by Access-Control-Allow-Headers.

Juqery Code
$.ajax({
cache: false,
type: type,
url: url,
data: JSON.stringify(data),
//async: false,
crossDomain: true,
//dataType: "jsonp",
processData: true,
contentType: 'application/json; charset=utf-8',
headers: {
'UserToken': 'sdsdsd'
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept,UserToken');
xhr.setRequestHeader('Access-Control-Allow-origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Credentials', "true");
},
success: successCallBack
});


Web API

XML
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,UserToken" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
  </customHeaders>
</httpProtocol>


[EnableCors(origins: "*", headers: "Origin, X-Requested-With, Content-Type, Accept, UserToken", methods: "*")]

config.EnableCors();

C#
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            System.Diagnostics.Debugger.Launch();

            // Helper.WriteLog("OnActionExecuted", DateTime.Now, DateTime.Now);
            try
            {
                string actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
                string controllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;

                if (controllerName != USERCONTROLLER && actionName != USERACTION)
                {
                    if (actionExecutedContext.Request.Content.Headers.GetValues("UserToken").First() != null)
                    {
                        string token = actionExecutedContext.Request.Content.Headers.GetValues("UserToken").First();
                        actionExecutedContext.Response.Content.Headers.Add("UserToken", token);
                        actionExecutedContext.Response.Content.Headers.Add("Access-Control-Allow-Origin", "*");
                        actionExecutedContext.Response.Content.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                    }
                }

            }
            catch (Exception)
            {
                actionExecutedContext.Response.Content.Headers.Add("UserToken", "");
                actionExecutedContext.Response.Content.Headers.Add("Access-Control-Allow-Origin", "*");
                actionExecutedContext.Response.Content.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            }

            base.OnActionExecuted(actionExecutedContext);
        }



UI and web api both are deployed in IIS 7.0. not working in Chrome and FireFox.
Posted
Comments
Kumarbs 8-Aug-14 0:23am    
Just check with this article
http://www.codeproject.com/Articles/800608/Creating-and-Consuming-Web-API-hosted-on-Azure-Emu
To enable cors properly. As you made enable cors, you don't require to put crossdomain=true.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900