由于目前的项目需要无刷新的跨域操作数据,整理了下自己使用的基于jQuery的Jsonp跨域[Get方式]。

代码如下:

Javascript部分

$(function(){
    $.ajax({
        async: false, // 同步加载数据,即等到ajax执行完毕再接着执行下面的语句
        url: '//www.123456.com/',
        type: 'GET', // jsonp模式只有GET是合法的
        data: {'act':'check','name':'Tom'}, // 预传参的数组
        dataType: 'jsonp', // 数据类型
        timeout: 5000, // 请求超时时间
        jsonp: 'backfunc', // 指定回调函数名,与服务器端接收的一致,并回传回来
        success: function(json){
            console.log(json);
        }
    });
});

服务器端的php代码

$act = trim($_GET['act']);
$name = trim($_GET['name']);
if($act == 'check' && $name == 'Tom'){
	echo trim($_GET['backfunc']).'('. json_encode(array('status'=>1,'info'=>'OK')) .')';	
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-12
猜你喜欢
  • 2022-01-05
  • 2021-08-10
  • 2022-12-23
  • 2021-07-20
  • 2021-10-10
  • 2021-09-27
相关资源
相似解决方案