yii框架跨域传值到laravel,接口调用

来源:互联网 发布:au2018破解版 mac 编辑:程序博客网 时间:2024/06/05 09:37

这是什么意思呢?疑问

我现在跟大家说一下题目内容:

  随着手机用户的越来越多,手机app也成为大家必不可少的应用软件产品,那么app的访问原理是什么呢?肯定不是直接查询数据库啦,那就是现在主流的一种方法----调用接口,下面,我就来展示一下怎么调用接口。

   前台yii主要作为展示渲染表单页面,后台laravel主要用于操作数据库,所做的功能有:

     (1)用户登录

     (2)数据展示,删除

     (3)加入回收站,数据还原,删除(加入回收站不能用于修改一个字段,要用独立一张表)

     (4)操作日志

     一.登录(yii)

<center>    <h2>登录页面</h2>        <table>            <tr>                <td>用户名:</td>                <td><input type="text" id="name"></td>            </tr>            <tr>                <td>密码:</td>                <td><input type="password" id="password"></td>            </tr>            <tr>                <td><input type="submit" value="登录"></td>                <td></td>            </tr>        </table></center><script src="../jquery.js"></script><script>    $(':submit').click(function(){        var name=$('#name').val();        var password=$('#password').val();//        alert(name);        $.ajax({            type: "POST",            url: "http://www.dandan.com:8080/php10_3/lianxi/book_jieko/books/public/Book/login_pro",//跳转地址为后台laravel登录验证            data: {name:name,password:password},            success: function(msg){                if(msg==1){                    location.href='index.php?r=show/lists';                }            }        });    });</script>
二.后台登录验证(laravel)

 //登录验证    public function login_pro()    {        $name = \Request::input('name');        $password = \Request::input('password');        $arr = DB::table('change')->where(['name' => $name, 'password' => $password])->first();        if ($arr) {            session_start();            $_SESSION['name'] = $name;            //给yii前台一个返回值,前台判断后跳转(如上)           return 1;        }    }
三.信息展示(yii)

<center>    <h2>展示页面</h2>    <div id="show"></div>    <a href="index.php?r=show/hs">查看回收站</a>||<a href="index.php?r=show/logs">查看日志</a></center><script src="../jquery.js"></script><script>        $.ajax({            type: "GET",            url: "http://www.dandan.com:8080/php10_3/lianxi/book_jieko/books/public/Book/show",//请求到后台            dataType:'json',            success: function(msg){//                alert(msg);             var trd="<table align='center' border='1'>";                trd +="<tr>";                trd +="<th>图书id</th>";                trd +="<th>图书名称</th>";                trd +="<th>图书简介</th>";                trd +="<th>图书作者</th>";                trd +="<th>图书价格</th>";                trd +="<th>操作</th>"                trd +="</tr>";                for(var i=0;i<msg.length;i++){                    trd +="<tr>";                    trd +="<td>"+msg[i].b_id+"</td>";                    trd +="<td>"+msg[i].name+"</td>";                    trd +="<td>"+msg[i].text+"</td>";                    trd +="<td>"+msg[i].writer+"</td>";                    trd +="<td>"+msg[i].price+"</td>";                    trd +='<td><a href="http://www.dandan.com:8080/php10_3/lianxi/book_jieko/books/public/Book/del?id='+msg[i]["b_id"]+'">删除</a></td>';                    trd +='<td><a href="http://www.dandan.com:8080/php10_3/lianxi/book_jieko/books/public/Book/up?id='+msg[i]["b_id"]+'">加入回收站</a></td>';                    trd +="</tr>";                }                trd +="</table>";                $("#show").html(trd);            }        });</script>
四.后台查询数据(laravel)

  //图书展示页面    public function show()    {        $arr = DB::table('book')->get();        echo json_encode($arr); //jeson加密后传值到前台,前台接到值后进行拼接展示     }

五.删除,添加日志入库(laravel)

 //展示页面删除    public function del()    {        $id = \Request::input('id');        $arr = DB::table('book')->where('b_id', '=', $id)->delete();        if ($arr) {            session_start();            $name = $_SESSION['name'];            $change = "删除id=.$id.的一条数据";            $time = date('Y-m-d H:i:s');            $re = DB::table('logs')->insert([                'l_name' => $name,                'l_change' => $change,                'l_time' => $time,            ]);            if ($re) {                $url="http://www.dandan.com:8080/php10_3/lianxi/book_jieko/show/web/index.php?r=show/lists";                echo  "<script>alert('删除成功');location.href='$url'</script>";//删除成功后再跳回前台展示页面            }else{                echo  "<script>alert('删除失败');</script>";            }        }    }

以上大概就是一些关于json跨域传值,调用接口的代码实现,大家感兴趣的话可以看一下,其中一些域名由于时间问题没变更,大家可以自己试一下


1 0
原创粉丝点击