【问题标题】:Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>
【发布时间】:2017-04-12 08:11:25
【问题描述】:

我在 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(),因为我需要将它从字符串转换为对象,希望有人可以帮助我解决这个问题,我真的很感激。

【问题讨论】:

  • data 应该已经是 JSON,所以你不需要JSON.parse(),用console.log(data)检查一次
  • 这通常发生在您的服务器端出现错误然后响应不是有效 json 的情况下,请先评论这一行 var offers = JSON.parse(data); 然后 console.log(data);您的错误将在控制台中
  • 我曾尝试删除 JSON.parse,但该功能根本不起作用
  • 你能发布一个返回 JSON 的例子吗?
  • Whats your data check network console or debug

标签: javascript php jquery json


【解决方案1】:

错误在您的服务器端,当您的服务器端出现错误时,响应带有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']
        );

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

【讨论】:

    【解决方案2】:

    当提供给 JSON.parse 的值实际上未定义时,通常会看到该错误。所以,我会检查试图解析这个的代码——很可能你没有解析这里显示的实际字符串。

    【讨论】:

      【解决方案3】:

      这是服务器端错误。您可以使用浏览器的开发工具(如 firefox)检查从服务器返回的值(php 代码)并检查响应消息。

      另外,也许你可以去掉 JSON.parse() 并在 ajax 函数中添加 dataType: "json"

      【讨论】:

        【解决方案4】:

        检查是否仅返回 json 编码的响应。如果您在 php 脚本中执行 var dump 之类的操作,请确保将其注释掉,因为它也会附加到您的 javascript 响应中

        【讨论】:

          【解决方案5】:

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

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

          这是我的 php 文件的开头: ο»Ώ

          【讨论】:

            猜你喜欢
            • 2021-09-30
            • 2016-10-24
            • 2021-07-20
            • 2022-01-04
            • 2018-08-27
            • 2018-07-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多