简体   繁体   English

未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> )

[英]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.

相关问题 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous> )</anonymous> - SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> ) 在 XMLHttpRequest.xmlhttp.onreadystatechange</anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.xmlhttp.onreadystatechange 未捕获到的SyntaxError:JSON中的意外令牌s在JSON.parse( <anonymous> ) - Uncaught SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM