• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP--------TP中的ajax请求

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

以jQuery中的ajax为例:

(1)引入jQuery

通过模板替换表示路径,我们可以自定义模板替换变量(在config中定义)

/*自定义模板替换标签*/

          'TMPL_PARSE_STRING' =>array(

          '__JS__'=>'/Think_php/Public/script'

          ),

(2)发送ajax请求(跟jquery中的ajax一样8步)

(3)ajax回应(ajaxReturn()方法)

     该方法会将控制器回应的数据进行json编码

      ajaxReturn('返回的数据','提示信息','操作状态')

      请求成功返回1,失败返回0

    ajaxReturn() ,控制器给我们返回数据进行json格式的编码

需要保证客户端接收的时候也要保证接受的数据格式是json格式的

dataType: json

默认就是json格式的

例:用户名的验证用ajax请求

public function ab(){
  $this->display();
  }
public function ajax(){
  //echo $_POST['username'];
  $user_model=M('User');
  $pass=$user_model->field('dept_id')->where("user_name='".$_POST['username']."'")->select();
  if($pass[0]['dept_id']!=$_POST['password']){
    //ajax回应(ajaxReturn()方法,该方法会将控制器回应的数据进行json编码,ajaxReturn('返回的数据','提示信息','操作状态'),请求成功返回1,失败返回0
   $this->ajaxReturn(array('wrong'),'failed',0);
   //$this->ajaxReturn('成功','success',1);
   //echo 'error';
   }else{
    //echo 'success';
    $this->ajaxReturn(array('right'),'success',1);
    //$this->ajaxReturn('失败','error',0);

    }
  }

 

tpl:


<form action="" method="post">
用户名:<input type="text" name="username" ><br />

密码:<input type="password" name="password" id='password'><br />
<input type="button"  ></script>

<script>
$(function(){
 $("#sub").click(function(){
  
  $.ajax({
   type:'POST',
   data:'username='+$("#username").val()+'&password='+$("#password").val(),
   url: '__URL__/ajax',
   async:true,
     dataType:'text',
   success:function(msg){
    alert(msg.data);<!--接收ajax函数返回的数据-->
    }
   
   })  
  })
 })
</script>

(1)引入jQuery

通过模板替换表示路径,我们可以自定义模板替换变量(在config中定义)

/*自定义模板替换标签*/

          'TMPL_PARSE_STRING' =>array(

          '__JS__'=>'/Think_php/Public/script'

          ),

(2)发送ajax请求(跟jquery中的ajax一样8步)

(3)ajax回应(ajaxReturn()方法)

     该方法会将控制器回应的数据进行json编码

      ajaxReturn('返回的数据','提示信息','操作状态')

      请求成功返回1,失败返回0

    ajaxReturn() ,控制器给我们返回数据进行json格式的编码

需要保证客户端接收的时候也要保证接受的数据格式是json格式的

dataType: json

默认就是json格式的

例:用户名的验证用ajax请求

public function ab(){
  $this->display();
  }
public function ajax(){
  //echo $_POST['username'];
  $user_model=M('User');
  $pass=$user_model->field('dept_id')->where("user_name='".$_POST['username']."'")->select();
  if($pass[0]['dept_id']!=$_POST['password']){
    //ajax回应(ajaxReturn()方法,该方法会将控制器回应的数据进行json编码,ajaxReturn('返回的数据','提示信息','操作状态'),请求成功返回1,失败返回0
   $this->ajaxReturn(array('wrong'),'failed',0);
   //$this->ajaxReturn('成功','success',1);
   //echo 'error';
   }else{
    //echo 'success';
    $this->ajaxReturn(array('right'),'success',1);
    //$this->ajaxReturn('失败','error',0);

    }
  }

 

tpl:


<form action="" method="post">
用户名:<input type="text" name="username" ><br />

密码:<input type="password" name="password" id='password'><br />
<input type="button"  ></script>

<script>
$(function(){
 $("#sub").click(function(){
  
  $.ajax({
   type:'POST',
   data:'username='+$("#username").val()+'&password='+$("#password").val(),
   url: '__URL__/ajax',
   async:true,
     dataType:'text',
   success:function(msg){
    alert(msg.data);<!--接收ajax函数返回的数据-->
    }
   
   })  
  })
 })
</script>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php审核操作发布时间:2022-07-10
下一篇:
【PHP】各种时间处理发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap