两个框架 一个前台一个后台 跨域请求

1、yii 登录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>
    <center>
<input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">

            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="nname" id="na"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="pwd" id="pa"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="登录" id="de"></td>
                </tr>
            </table>


    <ul id="lis">

    </ul>
    </center>
</body>
</html>
<script src="jquery-1.9.1.min.js"></script>
<script>
    $(function(){
        $("#de").click(function(){
            var name=$("#na").val();
            var pwd=$("#pa").val();
        
            $.ajax({
                type:"GET",
                //dataType:"json",
                url:"http://localhost/October/laravelandyii/laravel-v5.1.11/public/index/login",
                data:{name:name,pwd:pwd},                
                success: function(msg){
                    //alert(msg);
                     //alert( "Data Saved: " + msg );
                     if(msg=="ok"){
                         location.href="index.php?r=index/show";
                     }else{                     
                         alert('用户名或密码错误');
                         
                     }
                   }

            })

        

        })
    })
</script>

2、列表页

</pre><pre name="code" class="php"><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<center>
<h2>列表页面</h2>
<a href="index.php?r=index/wor">操作日志</a>
</center>
    <div id="di">
        
    </div>
</body>
</html>
<script src="jquery-1.9.1.min.js"></script>
<script>
    $(function(){
        $.ajax({
            type:"GET",
            dataType:"json",
            url:"http://localhost/October/laravelandyii/laravel-v5.1.11/public/index/lists",
            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].id+"</td>";
                            trd +="<td>"+msg[i].bname+"</td>";
                            trd +="<td>"+msg[i].bjie+"</td>";
                            trd +="<td>"+msg[i].bzuo+"</td>";
                            trd +="<td>"+msg[i].bprick+"</td>";
                            trd +='<td><a href="http://localhost/October/laravelandyii/laravel-v5.1.11/public/index/del?id='+msg[i]["id"]+'">删除</a></td>';
                        trd +="</tr>";
                    }
                    trd +="</table>";
                    $("#di").html(trd);

                    
            }
        })

    })
</script>


3、操作日志

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <center>
    <h2>日志列表</h2>
    <a href="index.php?r=index/show">返回</a>
            <div id="ta"></div>
    
    </center>
</body>
</html>
<script src="jquery-1.9.1.min.js"></script>
<script>
    $.ajax({
        type:'GET',
        dataType:"json",
        url:"http://localhost/October/laravelandyii/laravel-v5.1.11/public/index/work",
        success:function(msg){
            //alert(msg)

            var str="<table border='1'>";
                str +="<tr>";
                str +="<th>序号</th>"
                str +="<th>操作人</th>";
                str +="<th>时间</th>";
                str +="<th>操作内容</th>";
                str +="<th>操作</th>";
                str +="</tr>";
            for(var i=0;i<msg.length;i++){
                str +="<tr>";
                str +="<td>"+msg[i]['id']+"</td>";
                str +="<td>"+msg[i]['work_name']+"</td>";
                str +="<td>"+msg[i]['time']+"</td>";
                str +="<td>"+msg[i]['saves']+"</td>";
                str +='<td><a href="http://localhost/October/laravelandyii/laravel-v5.1.11/public/index/gc?id='+msg[i].bid+'">还原</a></td>';
                str +="</tr>";
              }
                  str +="</table>";
          $("#ta").html(str);
        }
    })
</script>

4、yii 控制器

<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;

class IndexController extends Controller{

    public function actionLogin(){
    
        return $this->renderPartial('login');
        
    }

    public function actionShow(){
        return $this->renderPartial('show');
    }
    public function actionWor(){
        return $this->renderPartial('work');
    }
}



?>

5、laravel 控制器

<?php
namespace App\Http\Controllers;
use DB;
use Log;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Session;

class IndexController extends Controller{

    public function login(Request $request){
        $name=$request->input('name');
        $pwd=$request->input('pwd');
   
        $re=DB::table('buser')->where("username",$name)->first();
 
        if($re){
            $request->session()->put('name',$name);
            echo "ok";
 
        }else{
       
            echo "no";
        }
    }

    public function lists(){
        $data=DB::select("select * from bbook where status=0");
        //print_r($data);die;
        exit(json_encode($data));

        }

    public function del(Request $request){
        $username=session()->get('name');
        //echo $username;die;
        $id=$request->input('id');
        //echo $id;
        $de=DB::table('bbook')->where('id',$id)->update(['status'=>1]);
        if($de){
            $time=date("Y-m-d H:i:s");
            $saves=$username."删除了id为"."$id"."的图书";
            $arr=array('work_name'=>$username,'time'=>$time,'saves'=>$saves,'bid'=>$id);
            $this->works($arr);
            $url = "http://localhost/October/laravelandyii/basic3/web/index.php?r=index/show";
            echo "<script>alert('删除成功!');location.href='$url';</script>";
        }else{
            echo "<script>alert('删除失败!');lacation.href='$url';</script>";
        }
    }

    public function works($arr){
        DB::table('bwork')->insert($arr);
    }
    public function work(){
        $wo=DB::table('bwork')->get();
        exit(json_encode($wo));
        
    }

    public function gc(Request $request){
        $username=session()->get('name');
        $id=$request->input('id');
        $up=DB::table('bbook')->where('id',$id)->update(['status'=>0]);
        if($up){
            $time=date("Y-m-d H:i:s");
            $saves=$username."还原了id为".$id."的图书";
            $arr=array('work_name'=>$username,'time'=>$time,'saves'=>$saves,'bid'=>$id);
            $this->works($arr);
            echo "<script>alert('还原图书成功!');window.history.go(-1)</script>";
        }else{
            echo "<script>alert('你已经还原了这本图书!');window.history.go(-1)</script>";
        }

    }
}



?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值