zoukankan      html  css  js  c++  java
  • PHP--------TP中的ajax请求

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

    以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" id="username"><br />

    密码:<input type="password" name="password" id='password'><br />
    <input type="button"  id="sub" value="提交">
    <script type="text/JavaScript" src="__JS__/jquery-1.4.2.min.js"></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>

  • 相关阅读:
    一卡通应用系统解决方案
    2013524 项目一
    射频RFID系统
    SIM操作系统
    python中eval, exec, execfile,和compile [转载]
    FastJSON学习
    坑爹的UICollectionView
    8位单片机编程的一些坑
    Android DownloadThread.run()学习
    蓝牙接收苹果手机通知 ANCS协议分析
  • 原文地址:https://www.cnblogs.com/mmady/p/6069498.html
Copyright © 2011-2022 走看看