pixeloft pixeloft - 1 year ago 151
JSON Question

Cannot Parse JSON for PhoneGap App

I've been trying to parse JSON for a PhoneGap app that is dynamically generated by a localStorage variable. The php is doing it's job, but the javascript isn't parsing the data and displaying.


header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');


// Get User ID
$email = $_GET['email'];
$useridinit="SELECT userid FROM users WHERE email='".$email."'";
$useridgrab=mysql_query($useridinit) or die(mysql_error());
$useridq = mysql_fetch_array($useridgrab);
$userid = $useridq['userid'];

$data = array();
$q = mysql_query("SELECT *
FROM Msg_Trans
LEFT JOIN Msg_Master ON Msg_Trans.msgid=Msg_Master.msgid
LEFT JOIN users ON Msg_Master.fromid=users.userid
WHERE Msg_Trans.toid='".$userid."' ORDER BY Msg_Trans.status DESC");

while ($row=mysql_fetch_object($q)){
echo json_encode($data);

My JS:

var email = localStorage.getItem('email');
var url = "http://anglertrack.pixeloft.com/mobile/conn/messages.php?callback=?&email="+email;


$.each(result, function(i, field){
var date=field.posted_date;
var from=field.fromid;
var subject=field.msgsub;
$("#messageList").append("<li><a class='messageLink' href='message.html?date="+date+"&from="+from+"&subject="+subject+"'><span class='message'>from:"+from+"</span><h2>"+ date + " </h2><p>"+ subject +"</p></a></li>");




<ul id="messageList">


Answer Source

In your URL you're defining the callback name (callback=?), which is unnecessary when using jQuery. Try using the code below.

var url = "http://anglertrack.pixeloft.com/mobile/conn/messages.php?email=heath@pixeloft.com";

$.getJSON(url, function(result) {
  $.each(result, function(i, field) {
    var date = field.posted_date;
    var from = field.fromid;
    var subject = field.msgsub;
    $("#messageList").append("<li><a class='messageLink' href='message.html?date=" + date + "&from=" + from + "&subject=" + subject + "'><span class='message'>from:" + from + "</span><h2>" + date + " </h2><p>" + subject + "</p></a></li>");
<ul id="messageList"></ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download