2013-09-25 212 views
0

我的PHP網頁正在返回一個JSON字符串。我寫了以下函數來獲取這些數據並在jQuery移動列表視圖中顯示它們。從php頁面獲取JSON數據

function LoadJsonDataFunction() 
{ 
    $.getJSON("my_web_page.php", function(obj) { 
    $.each(obj, function(key, value){ 
     $("ul").append("<li>"+value.fname+"</li>"); 
    }); 
    }); 
} 

這裏是我的列表視圖代碼:

<ul data-role=listview> </ul> 

我呼籲人們在身體標記功能

​​

,但是當我在執行程序顯示「取消定義」,並沒有數據。

然後我改變$ .getJSON()請求像這樣,然後它的工作完美。

$.getJSON("some_page_returning_same_json_string.json",function(obj) { ..... 

讓我知道我該如何解決這個問題。

PS。這是我的PHP頁面輸出..

{ 
    "employees":[ 
    { 
     "fname": "sdsdsd", 
     "lname": "sdsd", 
     "phone": "sdsd", 
     "gender": "female", 
     "dob": "1990-03-11", 
     "address": "03", 
     "nic": "erer", 
     "email": "erererer", 
     "empid": "ererere", 
     "designation": "sdsds", 
     "qualifications": "dsds" 
    } 
    ] 
} 

這裏是我的PHP代碼

<?php 
    header('Content-Type: application/json'); 
    /* 
    Following code will list all the employees 
    */ 

    // array for JSON response 
    $response = array(); 

    // include db connect class 
    require_once __DIR__ . '/db_connect.php'; 

    // connecting to db 
    $db = new DB_CONNECT(); 

    // get all employees from employees table 
    $result = mysql_query("SELECT * FROM emp_master") or die(mysql_error()); 

    // check for empty result 
    if (mysql_num_rows($result) > 0) { 
    // looping through all results 
    // employees node 
    $response["employees"] = array(); 

    while ($row = mysql_fetch_array($result)) { 
     // temp user array 
     $employee = array(); 
     $employee["fname"] = $row["fname"]; 
     $employee["lname"] = $row["lname"]; 
     $employee["phone"] = $row["phone"]; 
     $employee["gender"] = $row["gender"]; 
     $employee["dob"] = $row["dob"]; 
     $employee["address"] = $row["address"]; 
     $employee["nic"] = $row["nic"]; 
     $employee["email"] = $row["email"]; 
     $employee["empid"] = $row["empid"]; 
     $employee["designation"] = $row["designation"]; 
     $employee["qualifications"] = $row["qualifications"]; 

     //push single employee into final response array 
     array_push($response["employees"], $employee); 
    } 
    // success 
    // $response["success"] = 1; 
    // echoing JSON response 

    echo json_encode($response); 
    } else { 
    // no employees found 
    $response["success"] = 0; 
    $response["message"] = "No employees found"; 

    // echo no users JSON 
    echo json_encode($response); 
    } 
?> 
+0

你是如何返回來自PHP端的json數據...? –

+1

你是否從php返回json字符串和'json_encode/json_decode'? – Jai

+0

您是否試圖在JavaScript中添加另一個foreach? –

回答

2

更改行的代碼:

$.each(obj, function(key, value){ 

$.each(obj.employees, function(key, value){ 

「employees」包含在「obj」中,然後它包含數組。 「obj」不包含您正在查找的數組。

+1

非常感謝。現在它的運行..再次感謝你。 – san88

+0

@ user1511958不客氣。 – Arfeen

3

你有沒有寫正確的頭?如果不寫本作中,你的PHP第一行:

header('Content-Type: application/json'); 
+0

yes..i在您的評論後添加了標題,但仍在列表中顯示前一個「未定義」項目。 – san88

0

這是我用來開始JQM最好exemple,您可以檢查鏈接或使用下面

$('#ListPage').bind('pageinit', function(event) { 
    $.getJSON('some_php_or_json_file_link', function(data) { 
     $('#listView li').remove(); 
     $.each(data.items, function(index, item) { 
      $('#listView').append('<li><span>' + item.json_label1 + '</span></li>'); 
     }); 
     $('#listView').listview('refresh'); 
    }); 
});