一、
控制器中
public function add(){ $this->display(); }
public function ajaxSave(){
if(IS_AJAX){
echo '执行输入';
$model = M("tb_user");
$model->create();
$model->add();
echo '执行成功'; //这里会弹出一个框,把这两句一起输出
}
}
view中
add.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__/Common/jquery-1.8.3.min.js"></script> //引入jqery
<script>
$(function(){
$("#subFrom").click(function(){
//3个参数 js中的jq 获取所有的值 串行化 回调函数
$.post("{:U("index/ajaxSave")}",$("form").serialize(),function(data){
alert(data);
})
});
})
</script>
</head>
<body>
<form>
<input type="text" name="name" />
<input type="text" name="like_num" />
<input type="text" name="hobby" />
</form>
<button id="subFrom">提交</button>
</body>
</html>
===========================================
===========================================
二、
控制器中写
1 <?php
2 namespace AdminController;
3 header("Content-type:text/html;charset=utf-8");
4 use ThinkController;
5 class DataController extends Controller {
6 public function getAjax(){
7 if(IS_AJAX){
8
9 $data = 'ok'; //无
10 $this->ajaxReturn(['abcd'=>'abcd',"name"=>'zhangsan']);
11 }else{
12 echo 'hehe,不是AJAX请求';
13
14 }
15 }
16
17
18 public function send(){
19
20 $this->display();
21 }
22 }
view
send.html
1 <!DOCTYPE html>
7 <html>
8 <head>
9 <title>TODO supply a title</title>
10 <meta charset="UTF-8">
11 <meta name="viewport" content="width=device-width, initial-scale=1.0">
12 <script src='__ROOT__/business/Admin/view/js/jquery-1.8.3.min.js'></script> //引入jqery
13 <script>
14 $(function(){
15 $("#getAjax").click(function(){
//不写这个‘’也行 也是3个参数
16 $.get("{:U("Admin/data/getajax",'','')}",null,function(data){
17 $("<div>"+data.name+"</div>").appendTo("body");
18 })
19 });
20
21 });
22
23 </script>
24 </head>
25 <body>
26 <div>TODO write content</div>
27 <ul>
28 <li>__ROOT__</li>
29 <li>__APP__</li>
30 <li>__MODULE__</li>
31 <li>__CONTROLLER__</li>
32 <li>__PUBLIC__</li>
33 <li>__SELF__</li>
34 </ul>
35
36 <button id='getAjax'>点击请求</button> //点击后,在后面添加 刷新后就没了
37 </body>
38 </html>
三、其他
手册的方法
//调用这个方法
public function send(){
$data = 'ok'; //浏览器返回
"ok"
$data = ['abcd'=>'abcd',"name"=>'zhangsan']; //浏览器返回 {"abcd":"abcd","name":"zhangsan"}
$this->ajaxReturn($data);
}
$this->ajaxReturn($data,xml); //返回下面的
<think>
<abcd>abcd</abcd>
<name>zhangsan</name>
</think>
-------------------------------------------------------