I get a error when want use $.each value json in jquery, in the following are my js code and output json_encode and error. what do i do?
This all output from ‘url'(json_encode): http://pastebin.com/gEr0Aa3s
This is my ajax call that give me error:
$.ajax({
type: "POST",
dataType: "json",
url: 'url',
data: dataString,
cache: false,
success: function (data) {
$.each(data.guide, function (index, value) {
$('#guide_name').append('<div>'+ value.guide +'</div>');
});
$.each(data.residence, function (index, value) {
$('#residence_name').append('<div id="'+value.hotel_id+'">'+ value.name_re +'</div>');
});
$.each(data.reunits, function (index, value) {
$('#residence_name .li_show').append('<li>'+value.name+'</li>');
});
},
"error": function (x, y, z) {
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
})
Error:
An error has occured:<br>
[object Object]<br>
error<br>
Not Found<br>
The data you posted is not valid JSON.
What you need to work with your code is a structure like this:
Where you should have commas between each of the properties in the object (i.e., what I’ve put on the end of each line) you’ve got
} {. Also you’ve repeated “residence” and “reunits” several times, which is something you could achieve with a different structure but wouldn’t fit your ajax success code’s logic.