Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>

新手上路,请多包涵

我在 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 许可协议

阅读 594
2 个回答

错误在您的服务器端,当您的服务器端出现错误时,响应带有 html 标签 ‘<’ 当有错误时 php 将添加带有错误消息的标签。因此,您的 json 包含 html 标签,并且由于意外标签而变得无效。

错误在这个数组内

$eventstArray[] = array
        (
            'label'         => $eventsQuery2['eventTitle'];
            'venue'         => $eventsQuery2['venueName'];
            'category'      => $eventsQuery2['catDesc'];
            'price'         => $eventsQuery2['eventPrice'];
            'description'   => $eventsQuery2['eventDescription'];
        );

它应该是

$eventstArray[] = array(
            'label' => $eventsQuery2['eventTitle'],
            'venue' => $eventsQuery2['venueName'],
            'category' => $eventsQuery2['catDesc'],
            'price' => $eventsQuery2['eventPrice'],
            'description' => $eventsQuery2['eventDescription']
        );

(问题来源是描述值后面的 分号(;)。它应该只在数组的 _末尾_)

原文由 Masivuye Cokile 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您确定您的 php 代码正确但无法解析返回的 json 字符串,请选中此选项。

我有一个类似的问题。我发现我的 PHP 代码已保存为 UTF8,但没有 No-BOM 选项。这在文件的开头添加了三个额外的字符:ο»Ώ(十六进制:EF BB BF),导致返回的 json 字符串在开头有这些额外的字符。

这是我的 php 文件的开头:ο»Ώ < ?php include…

十六进制模式下的代码预览

原文由 Dimitrios 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题