简体   繁体   中英

php json_encode doesn't give pure object

This is probaly a really easy question but am struggling with this four about 2 hours now.

I have a php file:

$array = array(
        "id"=> 1, "firstName"=> "James", "lastName"=> "King", "managerId"=> 0, "managerName"=> "", "title"=> "President and CEO", "department"=> "Corporate", "cellPhone"=> "617-000-0001", "officePhone"=> "781-000-0001", "email"=> "jking@fakemail.com", "city"=> "Boston, MA", "pic"=> "James_King.jpg", "twitterId"=> "@fakejking", "blog"=> "http://coenraets.org"
    );
echo json_encode($array);

Now I want to give this an object like the following:

Object
blog: "http://coenraets.org"
cellPhone: "617-000-0001"
city: "Boston, MA"
department: "Corporate"
email: "jking@fakemail.com"
firstName: "James"
id: 1
lastName: "King"
managerId: 0
managerName: ""
officePhone: "781-000-0001"
pic: "James_King.jpg"
title: "President and CEO"
twitterId: "@fakejking"

Right now i'm getting the following response:

abort: (a)

always: ()

complete: ()

done: ()

error: ()

....

readyState: 4

responseText: "{"id":1,"firstName":"James","lastName":"King","managerId":0,"managerName":"","title":"President and CEO","department":"Corporate","cellPhone":"617-000-0001","officePhone":"781-000-0001","email":"jking@fakemail.com","city":"Boston, MA","pic":"James_King.jpg","twitterId":"@fakejking","blog":"http:\/\/coenraets.org"}"

setRequestHeader: (a,b)

state: ()

... 

__proto__: Object

I really don't know where to look and am probaly doing something wrong but I really have no idea.

UPDATE

js

var result = $.ajax({
          url: "http://localhost/cordova/employees/index.php?name="+ searchKey,
          context: document.body
        });
    console.log(JSON.parse(result.responseText));

The line console.log(JSON.parse(result.responseText)) gives me the following error:

 Uncaught SyntaxError: Unexpected token o

That's because you're looking at the XMLHttpRequest object. You need to parse out the response to get the object.

var obj = JSON.parse(xhr.responseText);

where xhr is the name of the object you're doing a console.log on.

$.ajax() makes an asynchronous HTTP request, so you can't just get the response right way. You have to wait for the response to come to you. The done() construct can be used to fire a callback when your response is ready.

var getEmployee = $.ajax({
    url: "http://localhost/cordova/employees/index.php?name="+ searchKey,
    context: document.body,
    dataType: 'json'
});
// the callback will get fired when the response is received
getEmployee.done(function (result) {
    console.log(result.blog);
});
// this will get fired immediately before the response is even received
console.log(getEmployee.responseText);  

It's simple feature. read about stdClass in PHP and modify your php code to:

$array = new stdClass();
$array->id = 1;
$array->firstName = "James";
//..... other fields
echo json_encode($array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM