无法显示数据.SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符

Unable to show data. SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

本文关键字:JSON 数据 1列 字符 意外 1行第 parse 显示 SyntaxError      更新时间:2023-09-26

我将一个变量从AJAX函数传递到PHP页面。AJAX函数是在点击按钮时调用的&一个值作为参数传递。AJAX函数将此变量传递到php页面to_php_page.php,从该页面向服务器发出HTTPGET请求。此变量作为参数传递。我们得到了许多城市数据作为回应。相应的响应被存储在不同的数组&被编码为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是正确的(使用tcpdump从数据中提取的jsonint进行检查),并且直到前一天,同样的代码也一直有效。该错误在Chrome中无法重现。在升级Firefox并清除缓存后,它也莫名其妙地消失了,它从未出现在另一台电脑上仍未更新的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"确实是一个意外的字符。