If a user scrolls to nearly the bottom more content shall be loaded with the help of an ajax request. This ajax request gets some data back (as a string in form of a json object - so it´s not an json object but only has to be parsed into one). Further Iactually use a plugin (JSON2HTML) which generates html after a template iterating through a json object.
JSON (before ajax):
var template = {"tag":"div","class":"parent","children":[
{"tag":"div","class":"tablee","style":"cursor:/*pointer*/;","children":[
{"tag":"table","class":"post","children":[
{"tag":"tbody","children":[
{"tag":"tr","children":[
{"tag":"td","class":"postby","children":[
{"tag":"img","class":"postppic","src":"propic\/${profilepic}","html":""}
]},
{"tag":"td","class":"postcontent","children":[
{"tag":"p","class":"postusername","children":[
{"tag":"a","class":"poster","href":"${username}-profile.php","html":"${username}"},
{"tag":"br/"},
{"tag":"span","class":"caption","html":"${caption}"}
]}
]},
...
var data = [
{
"username":"momo",
"profilepic": "momo_1.jpg",
"caption":"",
"likes": "0",
},
{
"username":"momo",
"profilepic": "momo_1.jpg",
"caption":"",
"likes": "0",
},
...
];
var str = json2html.transform(data,template);
$("#rein").append(str);
Now if the user scrolls down more shall be loaded, the string formed like a json object is now echoed by the php the number of posts we already have in the docment is sent to (so we don´t get the same posts again...).
jQuery (ajax request when nearly bottom reached):
$(document).ready(function () {
function loadMore()
{
var accountpar = $(".parent").length;
$("h1").text(accountpar);
$.ajax({
url: 'homemore.php',
type: 'post',
data: {
'account':accountpar
},
success: function(json) {
var str = json2html.transform(json,template);
$(str).insertAfter($('[class="parent"]').last());
$("#rein").append();
homejs();
}
});
$(window).bind('scroll', bindScroll);
}
function bindScroll(){
if($(window).scrollTop() + $(window).height() > $(document).height() - 75) {
$(window).unbind('scroll');
loadMore();
}
}
$(window).scroll(bindScroll);
});
And that works fine to until the success is reached and data should be received. (homejs() is the jquery function that is loaded after document.ready so the first jquery written in the document.) This data looks like that (according to my google chrome browser):
var data = [
{
"username":"momo",
"profilepic": "momo_1.jpg",
"caption":"",
"likes": "0",
},
{
"username":"momo",
"profilepic": "momo_1.jpg",
"caption":"",
"likes": "0",
},
...
];
So something was received but it couldn´t be parsed? Cause that´s what console log says (is it called so? like where you can see all the errors...):
VM5454:2 Uncaught SyntaxError: Unexpected token v
json2html.transform @ json2html.js:33
//Normalize strings to JSON objects if necessary
var obj = typeof json === 'string' ? JSON.parse(json) : json; //line 33
$.ajax.success @ home.php:423
var str = json2html.transform(datanew,template); //line 423
j @ jquery-1.11.3.min.js:2
k.fireWith @ jquery-1.11.3.min.js:2
x @ jquery-1.11.3.min.js:5
b @ jquery-1.11.3.min.js:5
So can anyone of you figure out why it isn´t working? I hope I provided enough of information... Please help :']