不墨迹,直接进主题。
tp框架实现ajax
首先,我们先做一个testajax.html用来显示页面(只是一个简单的下拉列表^_^)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script> </head> <body> <select id="rules"> </select> </body> </html>
接下来我们要去操作后台页面
//ajax public function testajax() { $this->show(); //输入地址显示页面 } public function ajaxchuli() { $n = D("rules"); //造父类 $attr = $n->select(); //查询 $this->ajaxReturn($attr); //可修改参数,默认是JSON }
咳咳,说好的ajax呢? (┬_┬)
好吧,其实html界面的全部代码是这样的:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script> </head> <body> <select id="rules"> </select> <script type="text/javascript"> $.ajax({ url:"__COMTROLLER__/ajaxchuli", <!--后台处理路径--> data:{}, type:"POST", dataType:"JSON", success: function(data){ var str = ""; for(a in data) { str = str+"<option value='"+data[a].code+"'>"+data[a].name+"</option>"; } $("#rules").html(str); } }) </script> </body> </html>
tp框架ajax到此结束,so easy!