简体   繁体   English

未捕获的语法错误:意外令牌<第1行

[英]Uncaught Syntax Error: Unexpected token < on line 1

I have a text file with details stored in the following format: 我有一个文本文件,其中的详细信息以以下格式存储:

Hayden:Australia:200:7800:45
Lara:West Indies:204:10800:47
Gilchrist:Australia:100:12800:45

My php code is something like this 我的PHP代码是这样的

<?php
    $fields = array("name","country","matches","runs","centuries");
    $file = fopen("players.txt", "r");
    $count = 0;
    $finalArr = array();
    while($players=fgets($file))
    {
        $players = trim($players);
        $temparr = array();
        $arr = explode(":",$players);
        //echo $arr;
        for($i=0; $i<count($arr); $i++)
        {   
            $temparr[$fields[$i]] = $arr[i];
        }
        $finalArr[$count] = $temparr;
        $count++;
    }
    fclose($file);
    $return = json_encode($finalArr);
    echo $return;
?>

When I try to access it using JSON.parse , I get this error: Uncaught SyntaxError: Unexpected token < 当我尝试使用JSON.parse访问它时,出现以下错误: Uncaught SyntaxError: Unexpected token <

This is my HTML side code: 这是我的HTML辅助代码:

if(xhr.readyState==4 && xhr.status==200)
            {
                var players = JSON.parse(xhr.responseText);
                alert(players);
            }

Where am I going wrong? 我要去哪里错了?

Your code produces a lot of warnings before printing the JSON output due to a missing $ in 由于缺少$ in,因此在打印JSON输出之前,您的代码会产生很多警告。

$temparr[$fields[$i]] = $arr[i];

where 哪里

$arr[i] 

should be 应该

$arr[$i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM