2012-01-19 50 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();