• .Net Ajax跨域请求总结


    导语

    之前写过一篇文章Ajax跨域请求COOKIE无法带上的解决办法,这两天正好好好的查了一下相关知识,做来总结一下

    一、传统

    ajax跨域访问是一个老问题了,解决方法很多,比较常用的是JSONP方法,JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全。
    即使使用jQuery的jsonp方法,type设为POST,也会自动变为GET。

    官方问题说明:

    "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.Note: This will turn POSTs into GETs for remote-domain requests.

    如果跨域使用POST方式,可以使用创建一个隐藏的iframe来实现,与ajax上传图片原理一样,但这样会比较麻烦。

    二、当跨域访问时,浏览器会发请求吗

    没有设置跨越,浏览器会返回

    XMLHttpRequest cannot load http://google.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://run.jsbin.io' is therefore not allowed access.

    在开发工具中Network并没有任何记录。但实际请求仍会被发送,只是在浏览器做了拦截。
    参考:Access-Control-Allow-Origin与跨域

    三、Access-Control-Allow-Origin来实现跨域访问

    随着跨域请求的应用越来越多,W3C提供了跨域请求的标准方案(Cross-Origin Resource Sharing)。IE8、Firefox 3.5 及其以后的版本、Chrome浏览器、Safari 4 等已经实现了 Cross-Origin Resource Sharing 规范,实现了跨域请求。所有和CORS相关的response header都是以“Access-Control-“为前缀的:

    • Access-Control-Allow-Origin(必须) 这个必须包含在所有合法的跨域请求的response中,其值要么是Origin header中的值,要么就是”*“允许任何域的请求。
    • Access-Control-Allow-Credentials(可选),默认情况下cookie是不包含在CORS请求中的,使用这个header将会指明要在CORS请求中包含cookie,它的有效值是true, 如果不需要cookie, 正确的做法不是将其值设为false, 而是根本就不要这个包含header.
    • Access-Control-Expose-Header(可选),XMLHttpRequest 2 object中有一个getResponseHeader()方法,用以返回特定的response header,但是它只能得到简单的响应header,如果想让客户端访问到其他的一些header, 必须设定这个 Access-Control-Expose-Header,它的值是以逗号分隔的你想暴漏给客户端的header。

    在服务器响应客户端的时候,带上Access-Control-Allow-Origin头信息。

    • 如果设置 Access-Control-Allow-Origin:*,则允许所有域名的脚本访问该资源。
    • Access-Control-Allow-Origin:http://www.phpddt.com.com,允许特定的域名访问。

    参考:

    四、Asp.Net封装Access-Control-Allow-Origin头信息

    1.Web.config

    在Web.config中加入统一的Access-Control-Allow-Origin返回头信息,是最原始,也是最直接的。无论是在老版本WebForm还是Mvc都适用。

    CORS on IIS7
    For Microsoft IIS7, merge this into the web.config file at the root of your application or site:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.webServer>  
            <httpProtocol>  
                <customHeaders>  
                    <add name="Access-Control-Allow-Origin" value="*" />  
                </customHeaders>  
            </httpProtocol>  
        </system.webServer>  
    </configuration>
    

    2.自定义Attribute,来增加头信息

    a)创建一个attribute

    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
            base.OnActionExecuting(filterContext);
        }
    }
    

    b)应用到Controller中的Action

    [AllowCrossSiteJson]
    public ActionResult YourMethod()
    {
        return Json("data");
    }
    

    参考:ASP.NET MVC 设置Access-Control-Allow-Origin

    3.Microsoft.AspNet.Cors

    使用Microsoft.AspNet.Cors包,这个是微软封装好的一个类库,原理和之前是一样的,有兴趣的话可以详细参考如下的文章配置:

  • 相关阅读:
    《整理的艺术》读后感
    就算神游 之二:行路 2
    VBA实现outlook自动发信 2
    使用Event的体会
    DevExpress Asp.net(9) ASPxHiddenField的特性与基本使用
    .net 启动窗休的设计总结
    DevExpress Asp.net(5) ASPxCloudControl的基本使用
    DevExpress Asp.net(7) ASPxTreeList的基本使用之一
    数据结构树形结构(1)
    数据结构树形结构(2)
  • 原文地址:https://www.cnblogs.com/Hai--D/p/6076777.html
Copyright © 2020-2023  润新知