I am attempting to populate my jquery mobile listview using ajax to get my list of movie titles by doing a php Mysql query with data in json. I successfully obtained the data in the correct json format, however in my javascript I get the error on line.... li.appendChild("a")
HTML
<div data-role="main" class="ui-content">
<ul data-role="listview" id="list" >
</ul >
</div>
getcatagories2_mobile.php
<?php
include("includes/connection.php");
$q="Comedy";
$resultActor = mysqli_query($con,"SELECT id,title, plot,catagory,release_date,rated FROM ".TBL_DATA." WHERE catagory LIKE '%".$q."%' ORDER BY title ");
$x= array();
while($rowMovie = mysqli_fetch_array($resultActor)) {
$x[]= array("Category" => $rowMovie['title']);
}
$jsonarray=json_encode($x);
echo $jsonarray;
?>
successful sample JSON format output
[{"Category":"22 Jump Street"},{"Category":"27 Dresses"}]
my called javascript function using ajax...
var xhr = new XMLHttpRequest();
xhr.open("GET", "getcatagories2_mobile.php", true);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
var list = document.getElementById("list");
var data= xhr.responseText;
var vec= JSON.parse(data);
vec.forEach(
function(ob)
{
var li = document.createElement("li");
var a = document.createElement("a");
var text = document.createTextNode("ob.Category");
a.appendChild(text);
a.setAttribute("href","#");
li.appendChild("a");
list.appendChild("li");
}
);
$('#list').listview('refresh');
}
}
xhr.send();
};
when I used google debug I get my error on line-> li.appendChild("a"), when I hover over var vec= JSON.parse(data), the data is shown correctly.
what I am trying to achieve is simply for my jquery mobile to execute as such
<div data-role="main" class="ui-content">
<ul data-role="listview" id="list" >
<li><a href="#">22 Jump Street</a></li>
<li><a href="#">27 Dresses</a></li>
</ul >
</div>
You should remove the quotes from your appendChild-calls, otherwise you pass a string to that function and it does not know how to handle that. Wihtout the quotes you pass the object-reference to the function.
li.appendChild(a);
list.appendChild(li);