自动生成jsonp数据
<?php namespace app\controllers; use Yii; use yii\filters\AccessControl; use yii\web\Controller; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; class ListController extends Controller { //商品列表 public function actionIndex(){ //查询数据库数据 $db=yii::$app->db; $data=$db->createCommand("select * from goods")->queryAll(); return $this->render('index',['data'=>$data]); } //商品详情页 public function actionXiang(){ //接收id $request=yii::$app->request; $id=$request->get('id'); //var_dump($id); //通过id查询商品详情 $db=yii::$app->db; $res=$db->createCommand("select * from goods where id=$id")->queryone(); $count=$res['g_count']+1; $db->createCommand("UPDATE goods set g_count=$count where id=$id")->execute(); //接收cookie的id $l_id=$_COOKIE['l_id']; //echo $l_id;die; //cookie的id和商品的id放入数组 $data=array( 'l_id'=>$l_id, 'id'=>$id, ); //判断重复值,删除g_p表(关系表)本条商品的用户id和cookie的id $db->createCommand()->delete("g_p",$data)->execute(); //添加g_p表(关系表)本条商品的用户id和cookie的id $db->createCommand()->insert("g_p",$data)->execute(); $re=$db->createCommand("select * from goods where id=$id")->queryone(); return $this->render('xiang',['res'=>$re]); } //生成json数据 public function actionJson(){ //加header头为json对象,不加为json数组 //header("content-type:application/json;charset=utf-8"); //获取回调函数名 $callback=$_GET['callback']; //$jsoncallback=htmlspecialchars($_REQUEST['jsoncallback']); //可以通过地址栏传值(转译) // echo $jsoncallback;die; // 调用数据库查询对象 $db=yii::$app->db; //判断g_p表,根据L_id查询总条数 //获取cookie 的id //$l_id=$_COOKIE['l_id']; //print_R($l_id);die; $count=$db->createCommand("select * from g_p where l_id=1 order by p_id desc limit 5")->queryAll(); $coun=count($count); //var_dump($count);die; // 判断条数 if($coun=5){ $data=$db->createCommand("SELECT * FROM goods join g_p on goods.id=g_p.id where l_id=1 ORDER BY p_id desc limit 5")->queryAll(); }else{ $data=$db->createCommand("select * from goods order by g_count desc limit 5")->queryAll(); } // 把PHP数组转成JSON字符串 exit($callback."(".json_encode($data).")"); // exit($c."(".json_encode($arr).")"); // $json_string = json_encode($data); // echo $json_string;die; //var_dump($json_string); //return $json_string; } //结束 } =============================== l另一域名下调用<?php ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>新闻首页</title> </head> <body> <center> <h4>欢迎来到xxx新闻网站</h4> <h4>xx网站推出新品</h4> <table id="table"> </table> </center> </body> </html> <script src="jquery.1.12.js"></script> <script> $(document).ready(function(){ $.ajax({ type: "GET", url: "http://www.shopa.com/web/index.php?r=list/json", dataType: "jsonp", success: function(msg){ var str="<tr><td>id</td><td>商品名称</td><td>商品价格</td><td>商品颜色</td></tr>" for (var i = 0; i < msg.length; i++) { str+="<tr><td>"+msg[i]['id']+"</td>" str+="<td>"+msg[i]['g_name']+"</td>" str+="<td>"+msg[i]['g_price']+"</td>" str+="<td>"+msg[i]['g_color']+"</td></tr>" }; $("#table").append(str); } }); }) // var url="http://www.shopa.com/web/index.php?r=list/json?callback=?"; // $.get(url,function(msg){ // alert('111'); // var length = msg.length; // var table=""; // for(var i=0;i<length;i++){ // for(k in msg[i]){ // table+="<tr><td>"+msg[i][k]+"</td></tr>"; // } // } // $("#table").append(table); // },'jsonp') // </script>