用JSONP方式跨域请求

JQUERY 中封装的JSONP请求方法

1.index.html 中的javascript

$(function(){

$.ajax({
   type: "GET",
   async:false,
   url: "http://www.vm.com:81/index.php", //跨域请求的地址
   data:{'birt':2016},
   dataType: 'jsonp',
   jsonp: 'jsoncallback',
   success: function(msg){
     alert("msg:"+JSON.stringify(msg));
   },
   error:function(msg){
           //alert("msg:"+JSON.stringify(msg));
   }
});
});

2.index.php

    $cb = $_GET["jsoncallback"];
    $birt = $_GET["birt"];
    $varArr = array("username"=>"lianruihong","age"=>20,"birt"=>$birt);
    echo $cb."(".json_encode($varArr).")";

 

posted on 2016-03-22 17:41  lianruihong  阅读(143)  评论(0编辑  收藏  举报

导航