一.请求接口

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $.ajax({
        type: "get",
        url:"http://nong.cn/?r=home/kai",
        dataType:'jsonp',
        data:'',
        jsonp:'callback',
        success:function(msg) {
            var str = new String();
            $.each(msg, function (i, n) {
                str += "<li><a href=''>" + n.n_name + "</a></li>";
            });

            $("#ul").html(str);
        }

    });

})
</script>

二.写个简单的接口

/**
 * jsonp 返回值
 * 
 **/
public function actionKai()
{
   $sql ="select * from nav where is_show=1";
      $data = \yii::$app->db->createCommand($sql)->queryAll();     
      // dd($data);

       //动态执行回调函数
       $callback=$_GET['callback'];
       $json = json_encode($data); #转换为json格式
      return $callback."($json)";
}

相关文章:

  • 2021-11-07
  • 2022-12-23
  • 2021-06-05
  • 2022-12-23
  • 2021-10-09
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-09
  • 2022-01-22
  • 2021-04-10
相关资源
相似解决方案