我在 JSON.parse() 中有一个错误,我有 .php 文件,其中包含从数据库中检索数据的方法和用于自动完成功能的 .js 文件,我的 .php 文件将数据作为字符串返回,我需要使用将其转换为对象JSON.parse()。
这是我的 php 文件
<?php
include_once("database_conn.php");
function request($conn)
{
$eventstArray = array();
$events = "SELECT *
FROM te_events,te_category,te_venue
WHERE te_events.venueID = te_venue.venueID
AND te_events.catID = te_category_catID
ORDER BY 1
";
$eventsQuery1 = mysqli_query($conn,$events) or DIE (mysqli_error($conn));
while($eventsQuery2 = mysqli_fetch_array($eventsQuery1))
{
$eventstArray[] = array
(
'label' => $eventsQuery2['eventTitle'];
'venue' => $eventsQuery2['venueName'];
'category' => $eventsQuery2['catDesc'];
'price' => $eventsQuery2['eventPrice'];
'description' => $eventsQuery2['eventDescription'];
);
}
return json_encode($eventstArray);
}
echo request($conn);
?>
这是我的 autoComplete.js 文件
$(document).ready(function()
{
'use strict';
$.ajax
({
method: "get",
url: "requestOffer.php"
})
.done(function(data)
{
var offers = JSON.parse(data);
// now we have the data attach the autocomplete
$('#EOffers').autocomplete
({
minLength:3,
source: offers,
select: function(event, ui)
{
$('#chosenEvent').text(ui.item.label);
$('#chosenEvent').text(ui.item.vanue);
}
});
});
});
我无法删除 JSON.parse() 因为我需要将它从字符串转换为对象,希望有人可以帮助我解决这个问题,我真的很感激。
原文由 richard houw 发布,翻译遵循 CC BY-SA 4.0 许可协议
错误在您的服务器端,当您的服务器端出现错误时,响应带有 html 标签 ‘<’ 当有错误时 php 将添加带有错误消息的标签。因此,您的 json 包含 html 标签,并且由于意外标签而变得无效。
它应该是
(问题来源是描述值后面的 分号(;)。它应该只在数组的 _末尾_)