如何将数组更改为对象并使用ajax json获取数据?

时间:2015-11-23 04:40:02

标签: php jquery json ajax

以下编码是php页面显示的结果:

    user_list.php:
    $myarray=array();

    $myjson = json_encode($myarray);
    echo $myuser->searchUser($myjson);

,html的结果是:

    [{"userID":"1","username":"\u9ec3\u9ec3\u9ec3",
      "sex":"F","password":"1bbd886460827015e5d605ed44252251",
      "emails":"test@test.com","regdate":"2015-11-03 00:00:00",
      "dob":"1994-11-02","educationID":"6","positionID":"1",
      "home":"12341234","mobile":"21800000",
      "address":"AC2 5\F Rm5501","grade":"Y1",
      "status":"0","office_tel":"41234123",
      "inviter":null,"inviter_relation":null,"believe":"0",
      "remark":null}]

我知道,这是一个不是对象的数组。那我怎么能在这样的其他页面中获取这些数据呢?

   $(".edituser").click(function () {
        var user = $(this).data("id");
        $.ajax({
                  url:"user_list.php",
                  data:"userID="+user,
                  type : "POST",
                  dataType: "json",
                  success:function(data){  
                  **console.log(data);**
                  },
                  error:function(xhr){
                    alert('Ajax request fail');
                  }
                  });
    });

如何获取ajax中的数据? THX

1 个答案:

答案 0 :(得分:1)

要获得userID值,您可以使用:

var userID = data[0].userID;

var userID = data[0]['userID'];