gpt4 book ai didi

javascript - 未捕获的语法错误 : Unexpected token < on line 1

转载 作者:行者123 更新时间:2023-11-28 07:46:01 28 4
gpt4 key购买 nike

我有一个文本文件,其详细信息以以下格式存储:

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

我的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;
?>

当我尝试使用 JSON.parse 访问它时,我收到此错误:Uncaught SyntaxError: Unexpected token <

这是我的 HTML 端代码:

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

我哪里出错了?

最佳答案

由于缺少 $,您的代码在打印 JSON 输出之前会产生大量警告

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

哪里

$arr[i] 

应该是

$arr[$i]

关于javascript - 未捕获的语法错误 : Unexpected token < on line 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27425622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com