简体   繁体   English

Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous>

[英]Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<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().我在 JSON.parse() 中有一个错误,我有一个 .php 文件,其中包含从数据库和 .js 文件中检索数据的方法,用于自动完成功能,我的 .php 文件将数据作为字符串返回,我需要通过使用将其转换为对象JSON.parse()。

this is my php file这是我的 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);
?>

and this is my autoComplete.js file这是我的 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);
                        }
                    });
                });
            });

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.我无法删除 JSON.parse() 因为我需要将其从字符串转换为对象,希望有人能帮我解决这个问题,我真的很感激。

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.错误在您的服务器端,当您的服务器端出现错误时,响应带有 html 标签“<”,当出现错误时,php 将添加带有错误消息的标签。 Therefore your json contains the html tags and becomes invalid because of unexpected tags.因此,您的 json 包含 html 标签,并且由于意外标签而变得无效。

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) (问题来源是描述值后面的分号(;)。它应该只在数组的末尾

That error is normally seen when the value given to JSON.parse is actually undefined.当赋予 JSON.parse 的值实际上未定义时,通常会出现该错误。 So, I would check the code that is trying to parse this - most likely you are not parsing the actual string shown here.所以,我会检查试图解析这个的代码 - 很可能你没有解析这里显示的实际字符串。

It's server side error.这是服务器端错误。 You can check the value returned from server(php code) using browser's developer tools like firefox and check the response message.您可以使用浏览器的开发工具(如 firefox)检查从服务器(php 代码)返回的值并检查响应消息。

In addition, maybe you can remove JSON.parse() and add dataType: "json" inside the ajax function另外,也许您可​​以删除 JSON.parse() 并在 ajax 函数中添加 dataType: "json"

Check if only the json encoded response is being thrown back only.检查是否仅返回 json 编码的响应。 If you do things like var dump in the php script make sure you comment it out as it also get attached to your javascript response如果您在 php 脚本中执行诸如 var dump 之类的操作,请确保将其注释掉,因为它也会附加到您的 javascript 响应中

Check this option if you are sure that your php code is correct but the returned json string cannot be parsed.如果您确定您的 php 代码正确但无法解析返回的 json 字符串,请选中此选项。

I had a similar problem.我有一个类似的问题。 What I found was that my PHP code had been saved as UTF8 but without the No-BOM option.我发现我的 PHP 代码已保存为 UTF8,但没有 No-BOM 选项。 That added three extra chars: ο»Ώ (hex: EF BB BF) at the beginning of the file causing the returned json string to have those extra chars at the beginning.这在文件的开头添加了三个额外的字符:ο»Ώ(十六进制:EF BB BF)导致返回的 json 字符串在开头包含这些额外的字符。

here is how the beginning of my php file: ο»Ώ < ?php include...这是我的 php 文件的开头: ο»Ώ < ?php include...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse的位置2 <anonymous> )在对象上。 <anonymous> (userhistory.js:23) - Uncaught SyntaxError: Unexpected token < in JSON at position 2 at JSON.parse (<anonymous>) at Object.<anonymous> (userhistory.js:23) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous> )</anonymous> - SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous>) Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json 未捕获到的SyntaxError:JSON中的意外令牌s在JSON.parse( <anonymous> ) - Uncaught SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM