/** * 订单接口 */ public function orderApi(){ $orderNum=Request::input('id'); $arr=DB::table('order')->where(['orderNUm'=>$orderNum])->first(); echo $_GET['jsoncallback']."(".json_encode($arr).")";die; //print_r($arr); } /** *用户浏览商品接口 */ public function goods(){ $id=Request::input('id'); $res=DB::table('goods')->where('u_id',$id)->get(); // print_r($res->u_goods); static $result = array(); foreach ($res as $k => $v) { if (!is_array($v->u_goods)) { $result[$k][] = $v->u_goods; }else{ getRes($v->u_goods); } } echo $_GET['jsoncallback']."(".json_encode($result).")"; }
/** *用户注册接口 */
public function register(){
$name=Request::input('name');
$pwd=Request::input('pwd');
$name1=isset($name)?$name:'1';
$pwd1=isset($pwd)?$pwd:'1';
$res=DB::table('user')->insert(['u_name'=>$name1,'u_pwd'=>$pwd1]);
if($res){ echo $_GET['jsoncallback']."(".json_encode(1).")";
}else{ echo $_GET['jsoncallback']."(".json_encode(0).")"; }}/** * 登陆接口 */public function login(){ $name=Request::input('name'); $pwd=Request::input('pwd'); //$name1=isset($name)?$name:'1'; //$pwd1=isset($pwd)?$pwd:'1'; $res=DB::table('user')->where(['u_name'=>$name,'u_pwd'=>$pwd])->first(); if($res){ echo $_GET['jsoncallback']."(".json_encode(1).")"; }else{ echo $_GET['jsoncallback']."(".json_encode(0).")"; }}
调用页面
view层数据<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <script type="text/javascript" src="http://localhost/app/public/js/jquery.js"></script> </head> <body> <center> <table> <tr> <td>用户名</td> <td><input type="text" id="name"></td> </tr> <tr> <td>密码</td> <td><input type="password" id="pwd"></td> </tr> <tr> <td><button class="submit">登陆</button></td> <td></td> </tr> </table> </center> </body> </html> <script> $('.submit').click(function(){ var name=$('#name').val(); var pwd=$('#pwd').val(); //alert(pwd) $.ajax({ url:"http://www.laravel.cn/index.php/login", type:"GET", dataType:"jsonp", jsonp:"jsoncallback", async:"false", data:{ name:name, pwd:pwd }, success:function(msg){ if(msg==1){ alert("登陆成功") }else{ alert("用户名或密码错误") } } }) }) </script>//注册也是同样的请求方式
跨域登陆,注册接口
最新推荐文章于 2023-12-21 10:58:33 发布