Php 使用jquery获取JSON
当我尝试使用getJSON显示数据时,什么也没有发生,Php 使用jquery获取JSON,php,jquery,json,Php,Jquery,Json,当我尝试使用getJSON显示数据时,什么也没有发生,$(“#child-left-container-2”)应该显示php文件中的数据。我哪里出错了?。。。下面是我的代码的简单示例 php文件 while($row = mysql_fetch_assoc($query)) { //code $array[] = "<div class='array-container-2' $style id='$id' data-sid='$sid' data-user='$user'&g
$(“#child-left-container-2”)
应该显示php文件中的数据。我哪里出错了?。。。下面是我的代码的简单示例
php文件
while($row = mysql_fetch_assoc($query)) {
//code
$array[] = "<div class='array-container-2' $style id='$id' data-sid='$sid' data-user='$user'>$details1</div>";
}
echo json_encode($array);
在jQuery的第二行中
var id = $('#container').find('.graphic-blank:visible').siblings().attr('id');
您正试图访问jQuery对象数组的id属性(同级对象()
)。您需要指定一个jQuery对象来获取$getJSON
函数的id
更新如果只有一个同级,请通过执行以下操作来指定
var id = $('#container').find('.graphic-blank:visible').siblings().eq(0).attr('id');
我一点也不想侮辱你,但你所做的是坏习惯。如果您从客户端创建HTML,您可以有更短的请求。此外,如果使用firebug,您可以看到输出JSON的图表。这真是一个漂亮的特点。您还可以检查它是否保存在Firebug的DOM部分下。祝你好运。在将对html元素的响应设置为使用console.log进行调试之前 我看不出您为
response=$(“#child-left-container-2”)创建了div
看看你有没有
<div id="child-left-container-2" />
并检查您的响应,可能是数组下的json。。。
尝试
console.log(data)
并用F12打开debbuger我不知道这是否能解决您遇到的问题,但这就是我解决这个特定问题的方法。
尝试一下这段代码,如果它有效,停止通过json发送html将是一个额外的好处
$key = 0;
while($row = mysql_fetch_assoc($query)) {
//code
$array[$key]['id'] = $id;
$array[$key]['sid'] = $sid;
$array[$key]['user'] = $user;
$array[$key]['content'] = $details1;
$array[$key]['style'] = $style;
$key++;
}
echo json_encode($array);
JS:
$(函数(){
$('.next_按钮')。单击(函数(){
var id=$('#container').find('.graphic blank:visible').sides().attr('id');
$.getJSON('fetchstack.php?id='+id,函数(数据){
$('#child-left-container-2').html('');//清除div
用于(数据中的var i){
var id=data[i].id;
var sid=data[i].sid;
var user=data[i]。用户;
var content=数据[i]。内容;
var style=data[i].style;
$('#child-left-container-2')。追加($('').addClass('array-container-2')属性('id',id)
.attr('data-sid',sid).attr('data-user',user).html(内容);
}
});
});
});
我承认,由于不知道$style是什么,也不知道它是如何呈现的,我无法将其添加到链中。如果您发布$style的内容,我可以更新anwser。
我希望这会有所帮助。为什么不简单地在PHP文件中回显HTML,然后用它填充容器呢
while($row = mysql_fetch_assoc($query)) {
echo "<div class='array-container-2' $style id='$id' data-sid='$sid' data-user='$user'>$details1</div>";
}
$.getJSON('fetchstack.php?id='+id,function(data) {
$('#child-left-container-2').html(data);
});
while($row=mysql\u fetch\u assoc($query)){
回显“$details1”;
}
$.getJSON('fetchstack.php?id='+id,函数(数据){
$('#child-left-container-2').html(数据);
});
这是我的解决方案,工作完美无瑕:
PHP:
HTML列表视图:
<div data-role="page" id="myPage"><div data-role="content" id="myContent">
//my HTML list tag
<ul data-role="listview" id="myList"></ul>
</div></div>
//我的HTML列表标签
JAVASCRIPT
//trigger script on show page
$('#myPage').live('pageshow',function (event) {
//get our JSON data
$.getJSON('path_to_your_php_json_generator_file_declared_upper',function(data){
//append our JSON data to a variable
var json_entries = data.items;
//for each JSON data, append it to our list as one element
$.each(json_entries,function(index,entry){
$('#myList').append('<li><a href="#">' + entry.title + '</a></li>');
//assuming we have a field named "title" in JSON data
});
//refresh the list for layout (style) loading
$('#myList').listview('refresh');
});
});
//显示页面上的触发器脚本
$('#myPage').live('pageshow',函数(事件){
//获取我们的JSON数据
$.getJSON('path_to_your_php_json_generator_file_declared_upper')函数(数据){
//将JSON数据附加到变量
var json_entries=data.items;
//对于每个JSON数据,将其作为一个元素附加到列表中
$.each(json_条目,函数(索引,条目){
$('#myList')。追加('');
//假设JSON数据中有一个名为“title”的字段
});
//刷新列表以加载布局(样式)
$('#myList')。列表视图('refresh');
});
});
这就是使用php文件生成的JSON数据在jQuery Mobile上填充列表的方式。
您可以将这种脚本应用于jQuery代码中的每个JSON解释器,甚至包括参数(id、类别等)
希望它能有所帮助!试着把它放在echo json_encode($array);之前。 最后进行退出调用以避免额外的输出
header('Content-type:application/json;charset=utf-8');
echo json_encode($array);
exit();
尝试
console.log()
在数据
对象上查看是否是dom插入或json get出现问题检查firebug控制台中的错误…这意味着您的PHP脚本中有错误,并且您没有收到json响应。没有侮辱!您是对的!可以在控制台中查看,但正如我在回答开始时所说的(这是我的解决方案)这对我来说很好。我使用它是因为我的应用程序在一个XCODE项目中,所以没有本机应用程序的控制台,我生成的所有JSON数据也在应用程序上查看。我非常关注我生成的JSON,我建议所有人都这样做。这种不好的做法非常可靠!提问者必须尝试一下。
<div data-role="page" id="myPage"><div data-role="content" id="myContent">
//my HTML list tag
<ul data-role="listview" id="myList"></ul>
</div></div>
//trigger script on show page
$('#myPage').live('pageshow',function (event) {
//get our JSON data
$.getJSON('path_to_your_php_json_generator_file_declared_upper',function(data){
//append our JSON data to a variable
var json_entries = data.items;
//for each JSON data, append it to our list as one element
$.each(json_entries,function(index,entry){
$('#myList').append('<li><a href="#">' + entry.title + '</a></li>');
//assuming we have a field named "title" in JSON data
});
//refresh the list for layout (style) loading
$('#myList').listview('refresh');
});
});
header('Content-type:application/json;charset=utf-8');
echo json_encode($array);
exit();