ajax返回

    
1.几种方式
public function getAjax(){ //$data = 'ok'; //$this->ajaxReturn($data); // 'ok' //$this->ajaxReturn(['aaa'=>'bbb','name'=>'zhangsan']); //{"aaa":"bbb","name":"zhangsan"} //$this->ajaxReturn(['aaa'=>'bbb','name'=>'zhangsan'],'XML'); //返回一个xml样式的 //$this->ajaxReturn('<script>alert("hello")</script>','EVAL'); //会弹出一个框,不写EVAL的话,直接输出 不会弹出 //可以按着ctrl键 点ajaxReturn 进去看看 }

二、实现

<?php
namespace Admin\Controller;
header("Content-type:text/html;charset=utf-8");
use Think\Controller;
class DataController extends Controller {
    public function getAjax(){
        if(IS_AJAX){
            $data = 'ok';
            $this->ajaxReturn(['abcd'=>'abcd',"name"=>'zhangsan']);
        }else{
            echo 'hehe,不是AJAX请求';
            
        }
    }
    
    
    public function send(){
        $this->display();
    }
}

send.html

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src='__ROOT__/business/Admin/view/js/jquery-1.8.3.min.js'></script> //引入JQ
        <script>
            $(function(){
                $("#getAjax").click(function(){
                    $.get("{:U("Admin/data/getajax",'','')}",null,function(data){
                        $("<div>"+data.name+"</div>").appendTo("body");
                    })
                });
                
            });
            
        </script>
    </head>
    <body>
        <div>TODO write content</div>
        <ul>
            <li>__ROOT__</li>
            <li>__APP__</li>
            <li>__MODULE__</li>
            <li>__CONTROLLER__</li>
            <li>__PUBLIC__</li>
            <li>__SELF__</li>
        </ul>
        
        <button id='getAjax'>点击请求</button> //点击后 会在下面显示张三  刷新后没有
    </body>
</html>
<!--/NetBeansPhp/tp/thinkphp02
/NetBeansPhp/tp/thinkphp02/index.php
/NetBeansPhp/tp/thinkphp02/index.php/Admin
/NetBeansPhp/tp/thinkphp02/index.php/Admin/Data
/NetBeansPhp/tp/thinkphp02/Public
/NetBeansPhp/tp/thinkphp02/index.php/admin/data/send-->

 

posted @ 2015-08-18 23:23  __Rain丶  阅读(157)  评论(0编辑  收藏  举报