js跨域问题及解决方法

1,引入跨域服务器的js文件:

<script type="text/javascript" src=\'#\'" href="http://app100627199.qzone.qzoneapp.com/js/lib/jquery.js?FrontVer1339041095"></script">http://app100627199.qzone.qzoneapp.com/js/lib/jquery.js?FrontVer1339041095"></script>
<script type="text/javascript" src=\'#\'" /js/v_sdk.js?FrontVer1339041095"></script>
<script type="text/javascript">


 var succ_callback=function(data)
 {
  DIALOG.alert.show({title:'提示',message:'领取礼包成功'});
 };
 var fail_callback=function()
 {
  DIALOG.alert.show({title:'提示',message:'领取礼包失败'});
 };
 
 
 VGame.init(VGame.GAME_RAVEN,VGame.PLATFORM_TENCENT,'${c.user_id|u}');
 
 VGame.Package.collect(succ_callback,fail_callback);
 
</script>

2,jquery的ajax发送跨域请求://step 2. make a post api.bangyouxi.com
   var VUtil = {
 QueryString : function(item){
  var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
  return svalue ? svalue[1] : svalue;
 }
}

var VGame = {
 GAME_XIAOBING : "g_xiaobing",
 GAME_CITY : "g_city",
 GAME_RAVEN : "g_raven",
 GAME_HONGYAN : "g_hongyan",
 GAME_YZWG : "g_yzwg",
 PLATFORM_TENCENT : "tencent",
 PLATFORM_RENREN : "renren",
 PLATFORM_WEIYOUXI : "weiyouxi",
 PLATFORM_FACEBOOK : "facebook",

 _game:'',
 _platform:'',
 _gid:'',

 Package:{
  collect: function(succ_callback, fail_callback){
   //step 1. parse from query string
   var package = VUtil.QueryString("byx_package");
   var vid = VUtil.QueryString("vid");
   

   if(!package || !vid)
    return;

   //step 2. make a post api.bangyouxi.com
   $.ajax({    
    type: "GET",
    dataType: "jsonp",
    data: {"of":"jsonp","package":package, "vid": vid, "app": VGame._game, "platform": VGame._platform, "gid": VGame._gid},
    crossDomain: true,
    url: 'http://robert.bangyouxi.com/api/v1/packages/consume.ngi',  
    success: function(data){
     
     if(data.ret == 0){
      succ_callback(data);
     }
     else{
      fail_callback();
     }
    }
   });
   
  }
 },

 init: function(game, platform, gid){
  this._game= game,
  this._platform = platform,
  this._gid = gid
 }
}

 

 

你可能感兴趣的:(JavaScript,跨域,function,服务器)