zoukankan      html  css  js  c++  java
  • 服务端通过设置 响应头允许其他域名跨域访问数据

    设置响应头:

    #允许跨域的域,*代表所有域都接收

    Access-Control-Allow-Origin: http://foo.example 

    #允许跨域执行的方法

    Access-Control-Allow-Methods: POST, GET, OPTIONS

    #允许跨域设置的头信息

    Access-Control-Allow-Headers: X-PINGOTHER

    以下是nginx的方法
    if ($request_method = 'OPTIONS') {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Credentials true;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            return 200;
        }
         if ($request_method = 'POST') {

            add_header 'Access-Control-Allow-Origin' *;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

         }

         if ($request_method = 'GET') {

            add_header 'Access-Control-Allow-Origin' *;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

         }

    javascript:调用实例

    在dev.int.weishi.mobil 的nginx上设置以上配置,javascript页面所属网站是www.shareach.com,通过跨域Post调用dev.int.weishi.mobil 上的数据

    调用代码是
    postData:function(data,success){
        var accountUrl = "http://dev.int.weishi.mobi/web"+rpc.urlUser;
        var jsonuserinfo = $.toJSON(data);
        jQuery.ajax({
            type : 'POST',
            contentType : 'application/json',
            url : accountUrl,
            data : jsonuserinfo,
            dataType : 'json',
            success : function(data) {
                if (data.code == 0) {
                    success(data.result);
                }else{
                    alert(data.msg);
                }
            },
            error : function(data) {
                alert("error")
            }
        });
    },

  • 相关阅读:
    二叉树的深度(剑指offer)
    平衡二叉树(剑指offer)
    平衡二叉树
    513. Find Bottom Left Tree Value(得到左下角的节点)(树的层次遍历)
    637. Average of Levels in Binary Tree(一棵树每层节点的平均数)(二叉树的层序遍历)
    145. Binary Tree Postorder Traversal(非递归实现二叉树的后序遍历)
    正则表达式式总结
    re模块
    生成器 生成器函数 列表推倒式 生成器表达式
    闭包,迭代器
  • 原文地址:https://www.cnblogs.com/yinpengxiang/p/2393658.html
Copyright © 2011-2022 走看看