Home > php教程 > php手册 > THINKPHP中使用ajaxReturn进行AJAX交互

THINKPHP中使用ajaxReturn进行AJAX交互

WBOY
Release: 2016-06-07 11:36:56
Original
1296 people have browsed it

有不少人反馈说在使用ThinkPHP时,不知道怎么使用$this->ajaxReturn与模板页进行AJAX交互,
下面我以我的个人博客(http://www.gouguoyin.cn/)的管理员登录为例来介绍下$this->ajaxReturn与模板页进行AJAX交互使用方法
首先看PHP控制器的处理,在application/Admin/Controller/LoginController.class.php控制器里加入登录验证方法//ajax检验管理员登录是否正确<br>     public function check()<br>     {<br>         $user = M('User');<br>         if(!IS_AJAX){<br>            $this->ajaxReturn(array(<br>                 'info' => '非法的请求方式'<br>             ));<br>        <br>         }<br>         $user_name     = I('user_name', '');<br>         $user_password = I('user_password', '');<br> <br>         $user_password = md5($user_password);<br> <br>         $filter = array(<br>             'user_name'     => $user_name,<br>             'user_password' => $user_password<br>         );<br> <br>         $user_info = $user->where($filter)->find();<br>         <br>         if (1 == $user_info['type']) {<br>             $this->ajaxReturn(array(<br>                 'info' => '你无权登录后台'<br>             ));<br>         }<br> <br>         if($user_info){<br>             // 更新登录ip<br>             $info['ip'] = get_client_ip();<br>             //更新登录时间<br>             $info['update_time'] = date('Y-m-d H:i:s', time());<br> <br>             $user->where(array('id' => $user_info['id']))->save($info);<br> <br>             session('user_id',$user_info['id']);<br>            <br>             $data = array(<br>                 'info' => 'ok',<br>                 'callback' => U('user/index/index')<br>             );<br>         }else{<br>             $data = array(<br>                     'info' => '登录失败,请检查登录名和密码是否正确'<br>             );<br> <br>         }<br> <br>         $this->ajaxReturn($data);<br>     }登录模板页$('.submit').click(function(event){<br>    //阻止表单默认提交事件<br>     event.preventDefault();<br>     var userName=$("#user_name").val();<br>     var userPassword=$("#user_password").val();<br>     if(userPassword==""||userName==""){<br>         alert('登录名与密码不能为空 ');<br>         $("#user_name").focus();<br>         return false;<br>     }else{<br>         var url = "{U('Admin/Login/check')}";<br>         $.post(url, { user_name:userName, user_password:userPassword}, function(msg){<br>         if(msg.info == 'ok') {<br>           //alert('登录成功,正在转向后台主页!');<br>           window.location.href = msg.callback;<br>         } else {<br>           alert(msg.info);<br>         }<br>       }, 'json').error(function(){<br>         alert("网络连接错误,请稍后再试");<br>       });<br> <br>     }<br>   })PS:为方便大家使用,特将所有涉及的代码打包上传,下载地址:http://www.gouguoyin.cn/php/60.html

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template