Javascript Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<a
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43363866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous>
提问by richard houw
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() 因为我需要将其从字符串转换为对象,希望有人能帮我解决这个问题,我真的很感激。
采纳答案by Masivuye Cokile
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.
错误在您的服务器端,当您的服务器端出现错误时,响应带有 html 标签“<”,当出现错误时,php 将添加带有错误消息的标签。因此,您的 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 endof array)
(问题来源是描述值后面的分号(;)。它应该只在数组的末尾)
回答by anis programmer
That error is normally seen when the value given to JSON.parse is actually undefined. So, I would check the code that is trying to parse this - most likely you are not parsing the actual string shown here.
当赋予 JSON.parse 的值实际上未定义时,通常会出现该错误。所以,我会检查试图解析这个的代码 - 很可能你没有解析这里显示的实际字符串。
回答by Asmin Jismun
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"
回答by anesupaul-developer
Check if only the json encoded response is being thrown back only. 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
检查是否仅返回 json 编码的响应。如果您在 php 脚本中执行诸如 var dump 之类的操作,请确保将其注释掉,因为它也会附加到您的 javascript 响应中
回答by Dimitrios
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. 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.
我有一个类似的问题。我发现我的 PHP 代码已保存为 UTF8,但没有 No-BOM 选项。这增加了三个额外的字符:ο?? (hex: EF BB BF) 在文件的开头导致返回的 json 字符串在开头有那些额外的字符。
here is how the beginning of my php file: ο?? < ?php include...
这是我的 php 文件的开头:ο?? < ?php 包括...


回答by Yordy SH
me manda el error en JSON.parse(data) no puedo editar mis listas
me manda el error en JSON.parse(data) no puedo editar mis listas
function mostrar(idcategoria)
{
$.post("../ajax/categoria.php?op=mostrar",{idcategoria : idcategoria}, function(data,status)
{
data = JSON.parse(data);
mostrarform(true);
$("#nombre").val(data.nombre);
$("#descripcion").val(data.descripcion);
$("#idcategoria").val(data.idcategoria);
})
}

