getJSON跨域SyntaxError問(wèn)題分析
昨天寫(xiě)一個(gè)功能:點(diǎn)擊手機(jī)驗(yàn)證的同時(shí)獲取json端的數(shù)據(jù)。
javascript代碼如下:
$(".check_mobile").click(function(){
var mobile = $('.mobile').val();
$.getJSON("http://www.test.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代碼如下:
<?php
if($_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;
最后完整代碼:
<?php
header("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
相關(guān)文章
Yii 訪問(wèn) Gii(腳手架)時(shí)出現(xiàn) 403 錯(cuò)誤
這篇文章主要介紹了Yii 訪問(wèn) Gii(腳手架)時(shí)出現(xiàn) 403 錯(cuò)誤的解決方法的相關(guān)資料,需要的朋友可以參考下2018-06-06
php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法
這篇文章主要介紹了php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
php結(jié)合GD庫(kù)簡(jiǎn)單實(shí)現(xiàn)驗(yàn)證碼的示例代碼
這篇文章主要介紹了php結(jié)合GD庫(kù)簡(jiǎn)單實(shí)現(xiàn)驗(yàn)證碼的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
PHP后臺(tái)備份MySQL數(shù)據(jù)庫(kù)的源碼實(shí)例
今天小編就為大家分享一篇關(guān)于PHP后臺(tái)備份MySQL數(shù)據(jù)庫(kù)的源碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Swoole?webSocket客服IM消息系統(tǒng)方案解析
這篇文章主要為大家介紹了Swoole?webSocket客服IM消息系統(tǒng)方案解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
thinkphp5.1 文件引入路徑問(wèn)題及注意事項(xiàng)
這篇文章主要介紹了thinkphp5.1 文件引入路徑問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Laravel5框架自定義錯(cuò)誤頁(yè)面配置操作示例
這篇文章主要介紹了Laravel5框架自定義錯(cuò)誤頁(yè)面配置操作,結(jié)合具體實(shí)例形式分析了Laravel5自定義錯(cuò)誤頁(yè)面的原理、操作步驟及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04

