想要将JSON字符串传递给javascript变量[JSON.parse(),eval()]对我来说不起作用,返回意外的令牌错误

想要将JSON字符串传递给javascript变量[JSON.parse(),eval()]对我来说不起作用,返回意外的令牌错误,javascript,ajax,jsonp,getjson,Javascript,Ajax,Jsonp,Getjson,当我回显我的php数组时,它显示如下 echo json_编码($marray) 显示 {“marray”:[{“lat”:“12.34”,“long”:“76.35”},{“lat”:“13.60”,“long”:“77.34”},{“lat”:“14.45”,“long”:“78.70”},{“lat”:“12.12”,“long”:“79.47”} 我在在线json格式化程序中检查了我的json_字符串(如上所示)…它没有显示任何错误 我正在使用ajax获取这个变量中的json字符串——x

当我回显我的php数组时,它显示如下

echo json_编码($marray)

显示

{“marray”:[{“lat”:“12.34”,“long”:“76.35”},{“lat”:“13.60”,“long”:“77.34”},{“lat”:“14.45”,“long”:“78.70”},{“lat”:“12.12”,“long”:“79.47”}

我在在线json格式化程序中检查了我的json_字符串(如上所示)…它没有显示任何错误

我正在使用ajax获取这个变量中的json字符串——xmlhttp.responseText

如果我打印该变量,它将显示与php echo语句中相同的ourput

但是如果我直接将上面的json字符串复制并粘贴到javascript变量中(不使用ajax响应),它会显示出来

[对象对象],[对象对象],[对象对象],[对象对象对象]

然后我可以使用点运算符从中获取数据

我不知道当json字符串作为php文件的响应存储到js变量中时会发生什么问题

有很多关于这些的线程,但我仍然无法解决我的问题…JSON.parse()nad eval()对我不起作用

我的php代码

    $sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$length=mysql_num_rows($result);

while($row=mysql_fetch_array($result))
{
$marray[$i] =

array(
"lat" => $row['lat'],
"long" => $row['long']
     );
$i++;
}
$dmarray=array("marray"=>$marray);
echo json_encode($dmarray);
请帮帮我……这个问题似乎很愚蠢,但却让我花了整整3天的时间失眠

    <script type="text/javascript">
function displayvalue()
{
m="xxx";
     var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
var h=xmlhttp.responseText;
document.getElementById("data").innerHTML="h";
}
}
xmlhttp.open("GET","array.php?q="+m,true);
xmlhttp.send(null);
}
 </script>

函数displayvalue()
{
m=“xxx”;
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
var h=xmlhttp.responseText;
document.getElementById(“数据”).innerHTML=“h”;
}
}
open(“GET”,“array.php?q=“+m,true”);
xmlhttp.send(空);
}
一个简单(有效)的测试示例供您参考:

PHP:


JavaScript(html):


函数togetdata(回调){
var-httpRequest;
httpRequest=新的XMLHttpRequest();
httpRequest.onreadystatechange=getresponse;
open('GET',“webpage.php”);
httpRequest.send();
函数getresponse(){
if(httpRequest.readyState==4){
if(httpRequest.status==200){
回调(httpRequest.responseText);
}否则{
警报(“请求错误”);
}
}否则{
}
}
}
函数objstr(obj){
var s=“”;
用于(obj中的var i){
var v=obj[i];
if(typeof v==“对象”){
v=objstr(v);
s+=i+“:
”+v+“
”; }否则{ s+=i+“:“+v+”
”; } } 返回s; } 函数processdata(resp){ var r=JSON.parse(resp); var m=document.getElementById(“msg”); m、 innerHTML=objstr(r); } 获取数据
你能发布你用来解析JSON的JavaScript代码吗?等等,我会发布的,谢谢你回答我的问题MichealMichael Sandino我添加了java脚本plz help Men不确定你想在那里做什么。您没有在任何地方使用响应..如果使用json.parse(),eval(),我将无法解析json。。。。它在json.parse中将错误显示为意外标记,但我在json.js文件的json.parse中得到意外标记。您似乎遇到了一个奇怪的错误。也许你应该先识别这个bug。您可以从一个最小化的工作版本(例如)开始,逐步向后添加更多代码,直到错误再次发生。我猜您可能包含了一些影响JSON.parse的旧JavaScript库。
<?php

$datastr = '{"marray":[{"lat":"12.34","long":"76.35"},{"lat":"13.60","long":"77.34"},{"lat":"14.45","long":"78.70"},{"lat":"12.12","long":"79.47"}]}';
$dataarray = json_decode($datastr);
die(json_encode($dataarray));

?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
function togetdata(callback) {
    var httpRequest;
    httpRequest = new XMLHttpRequest();

    httpRequest.onreadystatechange = getresponse;
        httpRequest.open('GET', "webpage.php");
    httpRequest.send();

    function getresponse() {
            if (httpRequest.readyState === 4) {
                if (httpRequest.status === 200) {
                    callback(httpRequest.responseText);
                } else {
                    alert("Request Error");
                }
            } else {
        }
    }

}

function objstr(obj) {
    var s = "";
    for (var i in obj) {
        var v = obj[i];
        if (typeof v == "object") {
            v = objstr(v);
            s += i + ":<br>" + v + "<br>";
        } else {
            s += i + ": " + v + "<br>";
        }
    }
    return s;
}

function processdata(resp) {
    var r = JSON.parse(resp);
    var m = document.getElementById("msg");
    m.innerHTML = objstr(r);
}

</script>

</head>
<body>
<button id="ajaxButton" onclick="togetdata(processdata)">To get data</button>

<span id="msg"></msg>
</body>