1

The following coding is the out come of an other page php echo

user_list.php:

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

and the result of html is:

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

As I know, here is a array not an object. So how i can get those data in other page like this?

$(".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');
                  }
                  });
    });

How get i get the data in ajax? thx

0

There are two methods to access the object.

1. Ajax response.

$.ajax({
  url:"your_file.php",
  type : "POST",
  data : your_data,
  dataType: "json",
  success:function(data){  
    // Retrieve the object
    var result = data[0];
    // Grab username from the object
    console.log(result['username']);
  },
  error:function(xhr){
    alert('Ajax request fail');
  }
});

2. Server side script

$yourArray = array();
$yourJson = json_encode($yourArray);
$userData  = $yourJson->searchUser($yourJson);
$jsonData = json_decode($userData);
// Ouput the inner contents
echo json_encode($jsonData[0]);
  • @YukwongTsang Glad to know, cheers :-) – Nikhil Nov 23 '15 at 17:11
0

Change the format in the following method.

$myuser->searchUser($myjson)

OR

Use the 0 index to get the JSON object.

resultData[0] //will return JSON object

I will recommend changing the format in PHP method. All the best!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.