DDR爱好者之家 Design By 杰米

昨天写一个功能:点击手机验证的同时获取json端的数据。

javascript代码如下:

$(".check_mobile").click(function(){
var mobile = $('.mobile').val();
$.getJSON("http://www.test.com/user.php"+mobile+"&format=json&jsoncallback=", function(data){
if (data.succ == 1) {
var html = "<input type='hidden' name='cityid' value='"+data.data.cityid+"'><input type='hidden' name='communityid' value='"+data.data.communityid+"'>";
$(".r_m").append(html);
}
});
});

user.php代码如下:

<"htmlcode">
header("Access-Control-Allow-Origin:http:www.test.com");
$b = json_encode($return);
echo "{$_GET['jsoncallback']}({$b})";
exit;

最后完整代码:

<"Access-Control-Allow-Origin:http:www.test.com");
if($_GET){
$mobile = $_GET['mobile'];
if ($mobile == '18831167979') {
$user = array(
'city' =>'石家庄',
'cityid' =>'1',
'community' =>'紫晶悦城',
'communityid'=>'1'
);
$sucess = 1;
$return = array(
'succ' =>$sucess,
'data' => $user
);
}else {
$sucess = 2;
$return = array(
'succ' =>$sucess
);
}
$b = json_encode($return);
echo "{$_GET['jsoncallback']}({$b})";
exit;
}
"Access-Control-Allow-Origin:http:www.test.com"); 代码,则会出现

XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.
如果少了 echo "{$_GET['jsoncallback']}({$b})"; 代码

在谷歌浏览器中:Uncaught SyntaxError: Unexpected token :
在火狐浏览器中:SyntaxError: missing ; before statement

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米