[英]Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)
I want to pass an array from PHP file to JavaScript and store in JavaScript array. 我想将数组从PHP文件传递到JavaScript并存储在JavaScript数组中。 Here's the JavaScript code:
这是JavaScript代码:
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 file: 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);
?>
I am getting this error: 我收到此错误:
Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (< anonymous >)
未捕获到的SyntaxError:JSON中的意外令牌<在JSON.parse位置0处(<匿名>)
1- You don't declare an array in this way: array(name)
1-您不会以这种方式声明数组:
array(name)
Try instead: $name = array()
or $name = []
. 请尝试:
$name = array()
或$name = []
。
2- And quote your include include 'dbconnect.php';
2-并引用您的include
include 'dbconnect.php';
3- Correct access to variable count
, at $name[count]=$res_array['name'];
3-正确访问变量
count
,位于$name[count]=$res_array['name'];
, count
should be $count
. ,
count
应为$count
。
And you're not actually looping it. 而且您实际上并没有循环它。 You should make
$count++
after saving to the $name
array. 保存到
$name
数组后,应制作$count++
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.