Javascript 未捕获的语法错误:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中
我想将一个数组从PHP文件传递到JavaScript,并存储在JavaScript数组中。下面是JavaScript代码:Javascript 未捕获的语法错误:意外标记<;在JSON.parse(<;匿名>;)的位置0处的JSON中,javascript,php,jquery,arrays,json,Javascript,Php,Jquery,Arrays,Json,我想将一个数组从PHP文件传递到JavaScript,并存储在JavaScript数组中。下面是JavaScript代码: xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200) {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
nameData = JSON.parse(xmlhttp.responseText);
console.log(xmlhttp.responseText);
}
}
xmlhttp.readyState=4;
xmlhttp.open("GET","mapPHPname.php?Zip="+zipcode,true);
xmlhttp.send();
PHP文件:
<?php
$zip=isset($_GET['Zip']);
include 'dbconnect.php';
$sql="Select `name` from doctor where `zip` LIKE $zip";
$result = mysql_query( $sql, $conn );
$num_rows = mysql_num_rows($result);
array(name);
if($num_rows>=1)
{
$count=0;
while($res_array = mysql_fetch_assoc($result))
{
$name[$count]=$res_array['name'];
}
}
else
{
$name[0]="kashyap";
}
echo json_encode($name);
?>
我得到这个错误:
未捕获的SyntaxError:JSON中位置0处的意外标记<
在JSON.parse(1-您不能这样声明数组:
array(name)
请改为尝试:$name=array()
或$name=[]
2-引用includeinclude'dbconnect.php'代码>
3-正确访问变量count
,位于$name[count]=$res_数组['name']
,count
应该是$count
而实际上你并不是在循环它。在保存到$name
数组后,您应该设置$count++
。responseText()中到底是什么?你试过console.log()吗?我猜你从服务器上得到了一些HTML或XML。如果有,这是一个SQL注入的教科书示例。您可以通过调整URL中的Zip
参数来编写自定义SQL。