0

我的 PHP 网页返回一个 JSON 字符串。我编写了以下函数来获取这些数据并将它们显示在 jQuery 移动列表视图上。

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>

我已经调用了body标签中的函数

<body onload="LoadJsonDataFunction()">

但是当我执行程序时,它显示“未定义”并且没有数据。

然后我像这样更改 $.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);
  }
?>
4

3 回答 3

3

你写了正确的标题吗?如果不将其写为 PHP 中的第一行:

header('Content-Type: application/json');
于 2013-09-25T09:56:37.440 回答
2

换行:

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

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

“员工”包含在“obj”中,然后它包含数组。“obj”不包含您正在查找的数组。

于 2013-09-25T10:28:49.107 回答
0

这是我用来开始 JQM 的最佳示例,您可以查看链接或使用下面的代码

$('#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');
    });
});
于 2013-09-25T10:01:44.213 回答