码迷,mamicode.com
首页 > Web开发 > 详细

用JSONP方式跨域请求

时间:2016-03-22 18:57:46      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

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).")";

 

用JSONP方式跨域请求

标签:

原文地址:http://www.cnblogs.com/lianruihong/p/5307769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!