[英]Unable to show data. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
I am passing a variable from an AJAX function to a PHP page. 我正在将变量从AJAX函数传递到PHP页面。 AJAX function is called on a button click & a value is passed as parameter.
单击按钮将调用AJAX函数,并将值作为参数传递。 AJAX function passes this variable to a php page to_php_page.php from where a HTTP GET request is made to the server.
AJAX函数将此变量传递到php页面to_php_page.php,从该页面向服务器发出HTTP GET请求。 This variable is passed as the parameter.
该变量作为参数传递。 We get many city datas as response.
我们得到许多城市数据作为回应。 The corresponding response is stored in different arrays & is encoded as JSON & passed back to the AJAX function.
相应的响应存储在不同的数组中,并编码为JSON,并传递回AJAX函数。 All these datas are shown at once.
所有这些数据都立即显示。
But, Instead of getting the response, I am getting an error. 但是,我没有得到响应,而是得到了一个错误。
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
AJAX Function 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 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);
?>
UPDATE: 更新:
JSON to be parsed: 要解析的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"
}
]
}
NB: Is there an alternative for the AJAX function I have used?? 注意:我使用过的AJAX功能是否有替代方法? I mean, another AJAX function.
我的意思是,另一个AJAX功能。 I use this all time.
我一直都用这个。 I am looking for a much better AJAX function.
我正在寻找更好的AJAX功能。
You may have two possible problem sources: 您可能有两个可能的问题来源:
1) Content-Type. 1)内容类型。 Specify content type and charset just before sending out JSON from PHP:
从PHP发送JSON之前,指定内容类型和字符集:
Header('Content-Type: application/json;charset=utf8');
(and ensure content is UTF8 encoded). (并确保内容是UTF8编码的)。
2) I have recently experienced your same problem. 2)我最近遇到了您遇到的同样问题。 I'm 100% sure the JSON was correct (checked with jsonlint from data lifted off the wire with tcpdump) and the same code had worked up to the day before.
我100%地确定JSON是正确的(通过jsonlint从数据中使用tcpdump进行了数据检查),并且相同的代码已经在前一天奏效。 The bug was not reproducible in Chrome.
该错误在Chrome中无法重现。 It also went unexplicably away after upgrading Firefox and clearing the cache, but it never presented itself on a still non-updated Firefox on another PC.
升级Firefox并清除缓存后,它也意外消失了, 但从未在另一台PC上仍未更新的 Firefox中出现过。 In some cases I noticed that both Firefox and Chrome appear to request a JSON packet but process an earlier version from the cache, but was not able to reproduce the phenomenon in any predictable way.
在某些情况下,我注意到Firefox和Chrome似乎都在请求JSON数据包,但从缓存中处理了早期版本,但是无法以任何可预测的方式重现该现象。 I do know though that to prevent it it is sufficient to add a non-repeating element to the query:
我确实知道,尽管要防止这种情况,向查询添加一个非重复元素就足够了:
xmlhttp.open("GET","to_php_page.php?namee="+n + "&ts=" + Date.now(), true);
This effectively disables caching for that request, so for large JSON resources that change rarely, it isn't really a good thing to do. 这有效地禁用了对该请求的缓存,因此对于很少更改的大型JSON资源,这并不是一件好事。
You ought to verify whether the cUrl request PHP side does return properly. 您应该验证cUrl请求PHP端是否正确返回。 You might have an error, possibly a cUrl warning of some kind issued by httpGet.
您可能有一个错误,可能是httpGet发出的某种cUrl警告。
Try opening the URL directly in the browser. 尝试直接在浏览器中打开URL。 If you see something like
如果您看到类似
PHP Warning: curl_exec: ... { (JSON) }
well, that of course is not JSON and JSON.parse() will complain that the "P" in "PHP Warning" is indeed an unexpected character. 好吧,当然不是 JSON,而JSON.parse()会抱怨“ PHP警告”中的“ P”确实是意外字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.