douben1891 2013-09-25 09:53
浏览 50
已采纳

从php页面获取JSON数据

My PHP web page is returning a JSON string. I wrote following function to get those data and display them on jQuery mobile listview.

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

Here is my listview code:

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

I have called to the function in the body tag

<body onload="LoadJsonDataFunction()">

but when I executing the program it displays "undefine" and no data.

then I change $.getJSON() request like this.then its working perfectly.

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

let me know how can i fix this.

PS. Here is my php page output..

{
  "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"
    }
  ]
}

Here is my php code

<?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);
  }
?>
  • 写回答

3条回答 默认 最新

  • dou4121 2013-09-25 10:28
    关注

    Change the line:

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

    with

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

    The "employees" contains in "obj" and then it contains the array. "obj" does not contain the array you are looking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上