且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置原始请求标头

更新时间:2022-04-05 06:21:56

总之:您不能.

MDN 中所述; Origin是一个禁止"标头,这意味着您无法以编程方式进行更改.

As described on MDN; Origin is a 'forbidden' header, meaning that you cannot change it programatically.

您需要将Web服务器配置为允许CORS请求.

You would need to configure the web server to allow CORS requests.

要启用CORS,请将其添加到您的Web.config

<system.webServer>   
    <!-- Other stuff is usually located here as well... -->
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />               
        </customHeaders>
    </httpProtocol>
<system.webServer>

或者,在Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        /* Some register config stuff is already located here */
    }

    // Add this method:
    protected void Application_BeginRequest()
    {
        HttpContext.Current.Response.AddHeader
            (name: "Access-Control-Allow-Origin", value: "*");            
    }
}