【问题标题】:How to change array to be object and get the data using with ajax json?如何将数组更改为对象并使用 ajax json 获取数据?
【发布时间】:2015-11-23 04:40:02
【问题描述】:

下面的代码是一个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 中获取数据?谢谢

【问题讨论】:

  • 它是一个数组,因为它以 [ ] 开头...您可以访问 data[0].userID 或 data[0].emails 等。

标签: php jquery json ajax


【解决方案1】:

要获取userID 值,可以使用:

var userID = data[0].userID;

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

【讨论】:

    猜你喜欢
    • 2020-05-31
    • 2013-11-27
    • 1970-01-01
    • 2017-09-19
    • 2017-09-20
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多