国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

getJSON跨域SyntaxError問(wèn)題分析

2024-05-04 23:24:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了getJSON跨域SyntaxError問(wèn)題分析,需要的朋友可以參考下
 
 

昨天寫一個(gè)功能:點(diǎn)擊手機(jī)驗(yàn)證的同時(shí)獲取json端的數(shù)據(jù)。

javascript代碼如下:

$(".check_mobile").click(function(){var mobile = $('.mobile').val();$.getJSON("http://www.huudi.com/user.php?mobile="+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代碼如下:

<?phpif($_GET){$mobile = $_GET['mobile'];if ($mobile == 'XXXX') {$user = array('city' =>'石家莊','cityid' =>'1','community' =>'紫晶悅城','communityid'=>'1');$sucess = 1;$return = array('succ' =>$sucess,'data' => $user);}else {$sucess = 2;$return = array('succ' =>$sucess);}echo json_encode($return);}?>

相應(yīng)如下:

問(wèn)題出來(lái)了:

在火狐瀏覽器中: SyntaxError: missing ; before statement

解決方法如下:

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

最后完整代碼:

<?phpheader("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;}?>

如果在 PHP 中少了 header("Access-Control-Allow-Origin:http:www.test.com"); 代碼,則會(huì)出現(xiàn)

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平定县| 翼城县| 武义县| 吴堡县| 深泽县| 临朐县| 开远市| 长乐市| 广安市| 遂宁市| 万全县| 永德县| 儋州市| 阳春市| 北碚区| 石楼县| 聂荣县| 贵南县| 阿拉善右旗| 社旗县| 台东县| 北安市| 邵东县| 大冶市| 巴东县| 兴宁市| 庆城县| 吴堡县| 伽师县| 禄丰县| 万山特区| 武宁县| 右玉县| 兖州市| 防城港市| 鲜城| 临洮县| 齐河县| 远安县| 五家渠市| 望城县|