2012-01-19 48 views
5

当我尝试用getJSON显示数据时什么也没有发生,$('#child-left-container-2')应该显示来自php文件的数据。我的错在哪里?...下面是我的代码的简要示例。getJSON with jquery

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的

$(function() { 
    $('.next_button').click(function() { 

var id = $('#container').find('.graphic-blank:visible').siblings().attr('id'); 

     $.getJSON('fetchstack.php?id='+id,function(data) { 

      $('#child-left-container-2').html(''); //clear div 

      $.each(data,function(i,result) { 
       $('#child-left-container-2').append(result); 
      }); 
     }); 

      }); 
}); 
+1

尝试'的console.log()''的对象data'上,看它是否是DOM插入或JSON,也都是有问题的错误Firebug控制台 –

+1

检查... – Rafay

+1

这意味着你的PHP脚本有错误,你没有收到JSON响应。 –

回答

0

在你的jQuery的第二行

var id = $('#container').find('.graphic-blank:visible').siblings().attr('id'); 

您尝试访问的jQuery对象的数组的id属性( siblings())。你需要指定一个jQuery对象来获取ID为您$getJSON功能

更新如果只有一个兄弟姐妹,做

var id = $('#container').find('.graphic-blank:visible').siblings().eq(0).attr('id'); 
+0

只有一个兄弟姐妹。 – user892134

0

指定这个我不侮辱你的意思在所有,但你所做的是不好的做法。如果您从客户端创建HTML,则可以缩短请求时间。此外,如果您使用萤火虫,您可以看到输出JSON的图表。它真的是一个漂亮的功能。你也可以检查它是否被保存在Firebug的DOM部分下。祝你好运。

+0

不侮辱!你是对的!它可以在控制台中查看,但正如我在回答开始时所说的(这是我的解决方案),它对我来说效果很好。我使用它是因为我的应用程序在XCODE项目中,所以没有生成原生应用程序的控制台和我生成的所有JSON数据也在应用程序上查看。我非常关注我生成的JSON和我的建议,都是为了做这件事。而这种不好的做法非常可靠! – aki

0

您设置为html用的console.log调试它元素的响应之前,

我没有看到你创建了一个div来response = $('#child-left-container-2') 检查,如果你有

<div id="child-left-container-2" /> 

,并检查您响应,maby the json in under array ... 尝试console.log(data)并打开debbuger与F12

1

我不知道这是否会解决您遇到的问题,但这会b e我如何解决这个特殊的问题。 试一试这段代码,如果有效,停止通过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:

$(function() { 
    $('.next_button').click(function() { 

     var id = $('#container').find('.graphic-blank:visible').siblings().attr('id'); 
     $.getJSON('fetchstack.php?id='+id,function(data) { 
      $('#child-left-container-2').html(''); //clear div 

      for (var i in data){ 
       var id  = data[i].id; 
       var sid  = data[i].sid; 
       var user = data[i].user; 
       var content = data[i].content; 
       var style = data[i].style; 
       $('#child-left-container-2').append($('<div />').addClass('array-container-2')attr('id', id) 
       .attr('data-sid', sid).attr('data-user', user).html(content); 
      }   
     }); 
    }); 
}); 

我得承认,不知道$的风格是什么,或者它如何似乎呈现我不能将它添加到链。如果您发布$ style的内容,我可以更新anwser。 我希望这可以帮助。

+0

提问者必须尝试此操作。 – VKGS

0

为什么不直接在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); 
}); 
0

嗯,这是我的解决方案和工作完美无瑕:

PHP:

<?php 

ini_set('magic_quotes_gpc', false); 
header('Content-type: text/plain'); 

     //DB connection credentials 
$dbhost = 'hostname'; 
$dbuser = 'database_username'; 
$dbpass = 'database_password'; 
$dbname = 'database_name'; 

// allow cross-browser acces 
header('Access-Control-Allow-Origin: *'); 

// query SQL 
$sql = "do your DB query SELECT what ever here"; 

    //do our thingies withouth hacks (SQL Injection, etc) 
try { 
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$dbh->query("set names utf8"); 
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$stmt = $dbh->query($sql); 
$json_export = $stmt->fetchAll(PDO::FETCH_OBJ); 
$dbh = null; 

    // return JSON format DATA 
echo '{"items":'. json_encode($json_export) .'}'; 
} catch(PDOException $e) { 

    // return error 
echo '{"error":{"text":'. $e->getMessage() .'}}'; 
} 

?> 

HTML LISTVIEW:

<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> 

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'); 
    }); 
}); 

这就是你如何使用json数据生成的php文件填充jQuery Mobile上的列表。 即使使用参数(id,category等),您也可以将这种脚本适应jQuery代码中的每个JSON解释器!

希望它有助于这样或那样!

0

试图在echo json_encode($array);之前放这个理由。 最后放了一个退出呼叫以避免额外的输出。

header('Content-type:application/json;charset=utf-8'); 
echo json_encode($array); 
exit();