zoukankan      html  css  js  c++  java
  • 关于跨域问题(包括端口和域名跨域)

    关于跨域问题(包括端口和域名跨域)

      1、C#语言跨域 我估计任何语言在返回头加这个都可以解决跨域问题    

    //完全跨域设置 包括跨域名和端口
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); //跨域名
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*");//跨端口
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
    //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");允许的请求类型
    var data = HttpContext.Current.Request.Form["data"].ToString();
    HttpContext.Current.Response.ContentType = "text/plain";
    HttpContext.Current.Response.Write(data);
    View Code

      2、PHP跨域 同一个思想

    <?php
    $data = $_POST["data"];
    //完全跨域设置 包括跨域名和端口
    header("Access-Control-Allow-Origin:*"); //跨域名
    header("Access-Control-Allow-Headers:*"); //跨端口
    header("Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
    header('Content-Type:text/plain');
    echo $data;
    View Code

      3、webApi跨域访问

           /// <summary>
            /// 跨域解决
            /// </summary>
            /// <param name="httpResponseMessage"></param>
            /// <returns></returns>
            protected HttpResponseMessage crossDomain(HttpResponseMessage httpResponseMessage)
            {
                httpResponseMessage.Headers.Add("Access-Control-Allow-Origin", "*");
                httpResponseMessage.Headers.Add("Access-Control-Allow-Headers", "*");
                httpResponseMessage.Headers.Add("Access-Control-Allow-Methods", "GET, POST");
                return httpResponseMessage;
    
            }
    View Code

      或者在Web.config配置自动发送默认请求头 在<system.webServer>  </system.webServer>之间

      <system.webServer>
    
        <!--跨域配置-->
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="*" />
            <add name="Access-Control-Allow-Methods" value="GET, POST" />
          </customHeaders>
        </httpProtocol>
        <!--跨域配置-->

    ajax跨域只要在返回头加上这三就OK了

    Access-Control-Allow-Origin:*
    Access-Control-Allow-Headers:*
    Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS

  • 相关阅读:
    用心合作
    添加IE右键菜单 以 调用和运行 自己的程序或文件
    VS2005 My.Computer.Registry 对象 操作注册表 简单示例
    项目经理职业生涯
    Visual studio.NET单元测试中Assert类的用法(转载)
    如何正确理解自动化测试?(转载)
    浅析ASP.NET单元测试中的调试(转载)
    软件项目质量管理实战总结(转)
    主题:小公司如何做项目管理(转)
    什么是“极限编程”?(转载)
  • 原文地址:https://www.cnblogs.com/zhouyukai/p/7491548.html
Copyright © 2011-2022 走看看