Could not add Access-Control-Allow-Origin to my WCF library Project

后端 未结 5 2117
借酒劲吻你
借酒劲吻你 2021-02-09 09:03

I\'m trying to understand why this ajax called doesn\'t work

 $.ajax({
        type: \'GET\',
        url: \"http://localhost:8732/Design_Time_Addresses/InMotion         


        
5条回答
  •  醉话见心
    2021-02-09 09:35

    I was getting the same problem when working with my WCF service directly in Visual Studio, in Chrome and Firefox. I fixed it with the following:

    Edit the Global.asax file with below function:

                private void EnableCrossDomainAjaxCall()
                {
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    
                    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                    {
                        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
                        HttpContext.Current.Response.End();
                    }
                }
    

    Then call the function from

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
           EnableCrossDomainAjaxCall();
        }
    

    You can get more information from the following url:

    http://blog.blums.eu/2013/09/05/restfull-wcf-service-with-cors-and-jquery-and-basic-access-authentication.

提交回复
热议问题