码迷,mamicode.com
首页 > Web开发 > 详细

ajax返回

时间:2015-08-19 00:19:18      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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

 

ajax返回

标签:

原文地址:http://www.cnblogs.com/rainblack/p/4740991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!