[英]SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data?
[英]Unable to show data. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
我正在將變量從AJAX函數傳遞到PHP頁面。 單擊按鈕將調用AJAX函數,並將值作為參數傳遞。 AJAX函數將此變量傳遞到php頁面to_php_page.php,從該頁面向服務器發出HTTP GET請求。 該變量作為參數傳遞。 我們得到許多城市數據作為回應。 相應的響應存儲在不同的數組中,並編碼為JSON,並傳遞回AJAX函數。 所有這些數據都立即顯示。
但是,我沒有得到響應,而是得到了一個錯誤。
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
AJAX功能
<script>
function get_data(n)
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() //callback fn
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
var jsonStr = JSON.parse(xmlhttp.responseText);
var count_array=parseInt(jsonStr.count);
var la=jsonStr.la;
var ny=jsonStr.ny;
var sj=jsonStr.sj;
var lv=jsonStr.lv;
var ut=jsonStr.ut;
var dc=jsonStr.dc;
var miami=jsonStr.miami;
var columbus=jsonStr.columbus;
var kc=jsonStr.kc;
var ch=jsonStr.ch;
for (i = 0; i < count_array; i++)
{
(function()
{
alert(la);
alert(ny);
alert(sj);
alert(lv);
alert(ut);
alert(dc);
alert(miami);
alert(columbus);
alert(kc);
alert(ch);
})();
}
}
}
xmlhttp.open("GET","to_php_page.php?namee="+n,true);
xmlhttp.send();
}
</script>
to_php_page.php
<?php
$namee_value=$_GET["namee"];
// Above variable is passed as argument with the url to obtain the response.
$url="server_url?nam=".$namee_value;
$jsondata= httpGet($url);
$details_array = json_decode($jsondata, true);
$count=count($details_array['country']);
function httpGet($url)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
//for storing los angeles details
$la=array();
for($i=0;$i<$count;$i++)
{
$la[$i]= $details_array['country'][$i]['los_angeles'];
}
//for storing new york details
$ny=array();
for($i=0;$i<$count;$i++)
{
$ny[$i]= $details_array['country'][$i]['new_york'];
}
//for storing san jose details
$sj=array();
for($i=0;$i<$count;$i++)
{
$sj[$i]= $details_array['country'][$i]['san_jose'];
}
//for storing las vegas details
$lv=array();
for($i=0;$i<$count;$i++)
{
$lv[$i]= $details_array['country'][$i]['las_vegas'];
}
//for storing utah details
$ut=array();
for($i=0;$i<$count;$i++)
{
$ut[$i]= $details_array['country'][$i]['utah'];
}
//for storing washington details
$dc=array();
for($i=0;$i<$count;$i++)
{
$dc[$i]= $details_array['country'][$i]['washington_dc'];
}
//for storing miami details
$miami=array();
for($i=0;$i<$count;$i++)
{
$miami[$i]= $details_array['country'][$i]['miami'];
}
//for storing columbus details
$columbus=array();
for($i=0;$i<$count;$i++)
{
$columbus[$i]= $details_array['country'][$i]['columbus'];
}
//for storing kansas city details
$kc=array();
for($i=0;$i<$count;$i++)
{
$kc[$i]= $details_array['country'][$i]['kansas_city'];
}
//for storing chicago details
$ch=array();
for($i=0;$i<$count;$i++)
{
$ch[$i]= $details_array['country'][$i]['chicago'];
}
$what_the_array = array(
'count' => $count,
'la' => $la,
'ny' => $ny,
'sj'=> $sj,
'lv'=>$lv,
'ut'=>$ut,
'dc'=>$dc,
'miami'=>$miami,
'columbus'=>$columbus,
'kc'=>$kc,
'ch'=>$ch
);
echo json_encode($what_the_array);
?>
更新:
要解析的JSON:
{
"country":
[
{
"los_angeles": "value1",
"new_york": "value2",
"san_jose": "value3",
"las_vegas": "value4",
"utah": "value5",
"washington_dc": "value6",
"miami": "value7",
"columbus": "value8",
"kansas_city": "value9",
"chicago": "value10"
}
]
}
注意:我使用過的AJAX功能是否有替代方法? 我的意思是,另一個AJAX功能。 我一直都用這個。 我正在尋找更好的AJAX功能。
您可能有兩個可能的問題來源:
1)內容類型。 從PHP發送JSON之前,指定內容類型和字符集:
Header('Content-Type: application/json;charset=utf8');
(並確保內容是UTF8編碼的)。
2)我最近遇到了您遇到的同樣問題。 我100%地確定JSON是正確的(通過jsonlint從數據中使用tcpdump進行了數據檢查),並且相同的代碼已經在前一天奏效。 該錯誤在Chrome中無法重現。 升級Firefox並清除緩存后,它也意外消失了, 但從未在另一台PC上仍未更新的 Firefox中出現過。 在某些情況下,我注意到Firefox和Chrome似乎都在請求JSON數據包,但從緩存中處理了早期版本,但是無法以任何可預測的方式重現該現象。 我確實知道,盡管要防止這種情況,向查詢添加一個非重復元素就足夠了:
xmlhttp.open("GET","to_php_page.php?namee="+n + "&ts=" + Date.now(), true);
這有效地禁用了對該請求的緩存,因此對於很少更改的大型JSON資源,這並不是一件好事。
您應該驗證cUrl請求PHP端是否正確返回。 您可能有一個錯誤,可能是httpGet發出的某種cUrl警告。
嘗試直接在瀏覽器中打開URL。 如果您看到類似
PHP Warning: curl_exec: ... { (JSON) }
好吧,當然不是 JSON,而JSON.parse()會抱怨“ PHP警告”中的“ P”確實是意外字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.