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

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

问题描述

我在JSON.parse()中有错误,我有.php文件,其中包含从数据库中检索数据的方法和.js文件中的autoComplete函数,我的.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(),因为我需要将其从字符串转换为对象,希望有人可以帮我解决这个问题,我真的很感激。

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:意外的令牌&lt;在位置0的JSON中:在Object处的JSON.parse(&lt; anonymous&gt;)处。&lt; anonymous&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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