TestController.class.php
<?php namespace HomeController; use ThinkController; class TestController extends Controller{ public function test() { if(empty($_POST)) { $this->display(); } else { $code = I('code'); $sheng = M('sheng'); $name = $sheng->where("sheng_id = '{$code}'")->find(); $this->ajaxReturn($name); } } } ?>
test.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="__PUBLIC__/JQ/jquery-1.12.4.min.js"></script> </head> <body> <div>代号:<input type="text" id="code"></div> <input type="button" value="显示" id="btn"> <div id="aa"></div> </body> </html> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(e) { var code = $("#code").val(); $.ajax({ url:"__SELF__", data:{code:code}, dataType:"json", type:"POST", success: function(data){ $("#aa").html(data.sheng_name); } }) }); }); </script>