未捕获到的SyntaxError:意外令牌& lt;在位置0的JSON中:在Object的JSON.parse(& lt; anonymous& gt;)处. [英] Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>

查看:103
本文介绍了未捕获到的SyntaxError:意外令牌& lt;在位置0的JSON中:在Object的JSON.parse(& lt; anonymous& gt;)处.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSON.parse()中出错,我有一个.php文件,其中包含从数据库检索数据的方法和用于自动完成功能的.js文件,我的.php文件以字符串形式返回数据,我需要将其转换为通过使用JSON.parse()来实现对象.

i have an error in JSON.parse(), i have .php file which contain method to retrieve data from database and .js file for autoComplete function, my .php file return data as string and i need to convert it to object by using 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文件

and this is my autoComplete.js file

$(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(),因为我需要将JSON.parse()从字符串转换为对象,希望有人可以帮助我解决这个问题,我对此表示感谢.

i can't remove the JSON.parse() because i need that to convert from string to object, hope someone can help me to solve this and i really appreciate that.

推荐答案

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

The error is within your server side, when there's an error on your server side, the response comes with html tags '<' when there's an error php will add tag with the error message. Therefore your json contains the html tags and becomes invalid because of unexpected tags.

错误在此数组内

The error is within this array

$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

(The problem source was the semi-colon(;) after the description value. It should be only at the end of array)

这篇关于未捕获到的SyntaxError:意外令牌&amp; lt;在位置0的JSON中:在Object的JSON.parse(&amp; lt; anonymous&amp; gt;)处.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆