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

  • 相关阅读:
    Java [Leetcode 319]Bulb Switcher
    Java [Leetcode 122]Best Time to Buy and Sell Stock II
    谱聚类算法
    Java [Leetcode 238]Product of Array Except Self
    Java [Leetcode 260]Single Number III
    X++ StrFix方法
    D365 FO第三方访问https证书问题
    D365 FO 使用.NET DLL
    D365 FO第三方集成(四)---客户端调用
    D365 FO第三方集成(三)---服务实现
  • 原文地址:https://www.cnblogs.com/zhouyukai/p/7491548.html
Copyright © 2011-2022 走看看