从AJAX调用返回的二维数组有一些问题,不确定不确定是否正确解析了JSON。

首先进行AJAX调用:

$.ajax({
    dataType: 'text',
    url: "./inc/datadmin.php?action=getCust&id="+id,
    success: function(data){
        var client = JSON.parse(data);
        for (var i = 0; i < client.cust.length; i++) {
            var counter = client.cust[i];
            console.log(counter.company_name);
            alert('me');
        }
    )};


在网络标签中看到的收益:

{
  "cust":
  {
    "company_id":"11",
    "company_name":"test",
    "company_owner_name":"testo",
    "company_owner_phone":"7038398608",
    "company_phone":"7038398608",
    "email":"[email protected]",
    "ein":"EIN Test",
    "mcin":"MCIN Test",
    "dot_license":"DOT Test",
    "ifta_license":"IFTA Test",
    "ifta_exp_date":"2015-05-15",
    "ky_ifta":"KY IFTA Test",
    "ky_ifta_exp_date":"2015-05-13",
    "nm_ifta":"NM IFTA Test",
    "nm_ifta_exp_date":"2015-05-11",
    "irp_license":"IRP Test",
    "irp_exp_date":"2015-05-11",
    "urc_license":"URC Test",
    "urc_total_vehc":"URC TRUCK Test",
    "urc_exp_date":"2015-05-23",
    "fleet_one":"FleetOne Test"
  },
  "permits":
  {
    "company_id":"11",
    "state":"IL",
    "permit_number":"232",
    "exp_date":"2015-05-28"
  }
}


您可以看到数据看起来正常,格式正确,是问题,但它从未进入过警报功能,实际上是对我说client.cust.length的长度为0?
即使我不是一个高级程序员,我也可以看到该循环至少应有1条记录。

oops忘记添加数据库的收益:

 $mdata = array();
$datas = $database->select("company", "*", array(
      "companyId" => $_REQUEST['id']
  ));
  foreach($datas as $data){
    $mdata['cust']=array(
            "company_id" => $data["companyId"],
          "company_name" => $data["company_name"],
          "company_owner_name" => $data["company_owner_name"],
          "company_owner_phone" => $data["company_owner_phone"],
          "company_phone" => $data["company_phone"],
          "email" => $data["email"],
          "ein" => $data["ein"],
          "mcin" => $data["mcin"],
          "dot_license" => $data["dot_license"],
          "ifta_license" => $data["ifta_license"],
          "ifta_exp_date" => $data["ifta_exp_date"],
          "ky_ifta" => $data["ky_ifta"],
          "ky_ifta_exp_date" => $data["ky_ifta_exp_date"],
          "nm_ifta" => $data["nm_ifta"],
          "nm_ifta_exp_date" => $data["nm_ifta_exp_date"],
          "irp_license" => $data["irp_license"],
          "irp_exp_date" => $data["irp_exp_date"],
          "urc_license" => $data["urc_license"],
          "urc_total_vehc" => $data["urc_total_vehc"],
          "urc_exp_date" => $data["urc_exp_date"],
          "fleet_one" => $data["fleet_one"],

    );
  }
  $dataz = $database->select("permits", "*", array(
         "companyId" => $_REQUEST['id']
   ));
   foreach($dataz as $data){
       $mdata['permits']=array(
        "company_id" => $data["companyId"],
        "state" => $data["state"],
        "permit_number" => $data["permit_number"],
        "exp_date" => $data["exp_date"]
       );
   }
 echo json_encode($mdata);


因此,“ cust”部分通过使用client.cust.company_id工作,但是“ permits”有效,我将数据库调用重做为:

   $dataz = $database->select("permits", "*", array(
         "companyId" => $_REQUEST['id']
   ));
   $mdata['permits']=array();
   $xt=1;
   foreach($dataz as $data){
        $mdata['permits'][$xt]['company_id'] = $data["companyId"];
        $mdata['permits'][$xt]['state'] = $data["state"];
        $mdata['permits'][$xt]['permit_number'] = $data["permit_number"];
        $mdata['permits'][$xt]['exp_date'] = $data["exp_date"];
        $xt++;
    }


数据返回如下:

"permits":
 {
 "1":
    {"company_id":"11",
    "state":"CA",
    "permit_number":"ertr",
    "exp_date":"2015-05-12"},
"2":
    {"company_id":"11",
    "state":"IL",
    "permit_number":"232",
    "exp_date":"2015-05-28"}
 }


但是我一直在alert(client.permits.length)上变得不确定
因此它永远不会进入:

for (var i = 0; i < client.permits.length; i++) {
              var counter = client.permits[i];
              console.log(counter.state);

    }

最佳答案

cust是一个对象,而不是数组。该对象没有length属性,这就是为什么从不输入循环的原因。

您可以直接访问cust上的属性:

alert(client.cust.company_name);


如果要遍历对象的键,则可以使用for...in loop代替。

var client = JSON.parse(data);

for (var key in client.cust) {
    if (client.cust.hasOwnProperty(key)) {
        alert(client.cust[key]);
    }
}

10-07 20:16