douxi3977 2017-04-12 08:11
浏览 686
已采纳

未捕获的SyntaxError:在位置0的JSON中出现意外的令牌:在对象的JSON.parse(<anonymous>)处。<anonymous>

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().

this is my php file

<?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);
?>

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);
                        }
                    });
                });
            });

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.

  • 写回答

5条回答 默认 最新

  • dqv84329 2017-04-12 08:20
    关注

    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'];
            );
    

    it should be

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决