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

后端 未结 6 995
Happy的楠姐
Happy的楠姐 2021-02-09 08:53

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

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


        
6条回答
  •  长发绾君心
    2021-02-09 09:45

    the solution is to create a file Global.asax

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, 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();
        }
    }
    

提交回复
热议问题