這是一個非常簡單的問題,但我現在正在爲這兩個小時苦苦掙扎。php json_encode不會給出純粹的對象
我有一個PHP文件:
$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"=> "[email protected]", "city"=> "Boston, MA", "pic"=> "James_King.jpg", "twitterId"=> "@fakejking", "blog"=> "http://coenraets.org"
);
echo json_encode($array);
現在我想這給像下面這樣的對象:
Object
blog: "http://coenraets.org"
cellPhone: "617-000-0001"
city: "Boston, MA"
department: "Corporate"
email: "[email protected]"
firstName: "James"
id: 1
lastName: "King"
managerId: 0
managerName: ""
officePhone: "781-000-0001"
pic: "James_King.jpg"
title: "President and CEO"
twitterId: "@fakejking"
現在,我發現了以下回應:
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":"[email protected]","city":"Boston, MA","pic":"James_King.jpg","twitterId":"@fakejking","blog":"http:\/\/coenraets.org"}"
setRequestHeader: (a,b)
state:()
...
__proto__: Object
我真的不知道去哪裏看,很可能做錯了什麼,但我真的不知道。
UPDATE
JS
var result = $.ajax({
url: "http://localhost/cordova/employees/index.php?name="+ searchKey,
context: document.body
});
console.log(JSON.parse(result.responseText));
線的console.log(JSON.parse(result.responseText))給了我以下錯誤:
Uncaught SyntaxError: Unexpected token o
響應文本是你的JSON對象。其餘的東西是圍繞請求發出的上下文。 –
你是否設置了標題類型? – Kisaragi
你的JavaScript代碼是什麼樣的? – MonkeyZeus