Fork me on GitHub

logo我的个人地带

努力每一天,每天进步一点点,每天快乐一点点

asp.net 设置跨域访问

在 web.config 中添加 (全局)

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
            <add name="Access-Control-Allow-Headers" value="x-requested-with"/>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
    </httpProtocol>
</system.webServer>


设置单个一般处理程序可以跨域

context.Response.ClearHeaders();
context.Response.AppendHeader("Access-Control-Allow-Origin","");
string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"];
context.Response.AppendHeader("Access-Control-Allow-Headers",
string.IsNullOrEmpty(requestHeaders) ? "" : requestHeaders);
context.Response.AppendHeader("Access-Control-Allow-Methods", "POST, OPTIONS");


Comments 0