Thinkphp部分代码:
<?php namespace YxgController; use ThinkController; class IndexController extends Controller{ public function index(){ $SongName = D('Song'); //数据查询 $data = $SongName->where('id=2')->find(); //ajax返回 if($data){ $this->ajaxReturn($data); }else{ $data = "ERROR"; $this->ajaxReturn($data); } } }
jQuery部分:
<!DOCTYPE html> <html> <head> <title></title> <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script> </head> <body> <div id="div1"></div>> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: '{:U('Index/index')}', type: 'POST', dataType: 'json', success: function(data){ $("#div1").html(data); } }); }); </script> </body> </html>
结果返回json数据:
效果图: