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


    刚刚在查资料的时候看到一篇对自己今后有的文章,是关于TP框架的,将就吧它摘下来了。

    以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>

  • 相关阅读:
    理解-NumPy
    python % format
    计算机三级网络技术考过指南
    jquery 增加与删除数组元素
    jquery 改变标签可见状态的几种方式
    pL/SQL 创建DBLIKN
    Oracle 数据类型详解
    状态模式例子---流程控制
    jqGrid 将行的字变成超连接
    表单提交详细介绍
  • 原文地址:https://www.cnblogs.com/yuyu1993/p/5722079.html
Copyright © 2020-2023  润新知