我一直在尝试为一个由localStorage变量动态生成的PhoneGap应用解析JSON。 php正在做它的工作,但是javascript并没有解析数据并显示。
我的PHP:
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
include("conn.php");
// 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)){
$data[]=$row;
}
echo json_encode($data);
我的JS:
var email = localStorage.getItem('email');
var url = "http://anglertrack.pixeloft.com/mobile/conn/messages.php?callback=?&email="+email;
$.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>");
});
});
我的HTML:
<ul id="messageList">
</ul>
答案 0 :(得分:2)
在您的URL中,您定义了回调名称(callback=?
),这在使用jQuery时是不必要的。请尝试使用以下代码。
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>