admin管理员组文章数量:1221166
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.
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.
Share Improve this question asked Apr 12, 2017 at 8:11 richard houwrichard houw 971 gold badge1 silver badge7 bronze badges 5 |5 Answers
Reset to default 3The 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)
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.
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.
In addition, maybe you can remove JSON.parse() and add dataType: "json" inside the ajax function
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
Check this option if you are sure that your php code is correct but the returned json string cannot be parsed.
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.
here is how the beginning of my php file: ο»Ώ < ?php include...
本文标签:
版权声明:本文标题:javascript - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) a 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739314689a2157745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data
should already be JSON so you don't needJSON.parse()
, check it once withconsole.log(data)
– Satpal Commented Apr 12, 2017 at 8:12var offers = JSON.parse(data);
then console.log(data); your error will be in the console – Masivuye Cokile Commented Apr 12, 2017 at 8:15