跨域jsonp

自动生成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();
              //接收cookieid
             $l_id=$_COOKIE['l_id'];
             //echo $l_id;die;
             //cookieid和商品的id放入数组
             $data=array(
               'l_id'=>$l_id,
               'id'=>$id,
              );
              //判断重复值,删除g_p(关系表)本条商品的用户idcookieid
               $db->createCommand()->delete("g_p",$data)->execute();
               //添加g_p(关系表)本条商品的用户idcookieid
               $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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值