美文网首页
【小白技巧】两种简单的跨域方法

【小白技巧】两种简单的跨域方法

作者: 活叁黄辉冯 | 来源:发表于2017-08-11 20:14 被阅读0次

    【注:小白使用电脑win10 64位】

    一、apache+php代理

    1、下载安装apache:http://www.apachelounge.com/download/

    2、下载安装php:http://windows.php.net/download/#php-5.5

    3、配置apache的步骤就不多说了,都是大同小异【相关步骤】

    4、代码部分 

    例:get方法【接口来源-->聚合数据-QQ号码测吉凶(免费)

    jq部分:

    $.ajax({

       url: 'demo.php?url=http://japi.juhe.cn/qqevaluate/qq?key='+appKey+'&qq='+qqnumber,

       type: 'get',

       dataType: 'json',

       cache: false,

       success: function(data) {},

       error:function(data){}

    })

    php部分:

    <?php

    $link = $_GET["url"];

    $crossUrl = $link;

     echo file_get_contents($crossUrl);

    ?>

    对,就是这么简单,只要按照步骤配置好apache,启动之后,写好短短的这几行代码,跨域访问就成功了!未完待续。。。

    二、nginx反向代理

    1、下载安装nginx:http://nginx.org/en/download.html

    2、配置nginx的步骤就不多说了,都是大同小异【相关链接】

    3、反向代理步骤:

    a、找到nginx文件夹中conf文件夹下的nginx.conf;

    b、找到如下代码:

          location / {

               root  html;

               index  index.html index.htm;

           }

    c、在其后加上如下代码

         #lhy为代理的名称

         location /lhy {

              rewrite  ^.+lhy/?(.*)$ /$1 break;

              include  uwsgi_params;

              #需要跨域的域名【接口来源-->聚合数据-QQ号码测吉凶(免费)

              proxy_pass  http://japi.juhe.cn;

        }

    4、启动nginx

    jq ajax请求:

    $.ajax({

      url: '/lhy/qqevaluate/qq?key='+appKey+'&qq='+qqnumber

       type: 'get',

       dataType: 'json',

       cache: false,

       success: function(data) {},

       error: function(data) {}

    })

    对,就是这么简单,只要按照步骤配置好nginx,启动之后,写好短短的这几行代码,跨域访问就成功了!未完待续。。。

    注:nginx启动、关闭小技巧【自己编辑bat文件】

    1、新建txt文件

    2、编辑

    a、启动代码:

         F:     ::nginx所在盘名称

         cd nginx-1.13.4   ::nginx所在盘路径

         start nginx.exe  ::启动nginx

    b、关闭代码:

          taskkill /f /im "nginx.exe"

    3、文件另存为.bat

    4、双击使用

    相关文章

      网友评论

          本文标题:【小白技巧】两种简单的跨域方法

          本文链接:https://www.haomeiwen.com/subject/cxcrrxtx.html