我正在尝试访问对象key
,但是它返回undefined
。这是代码
axios.post(url, {section: allSection, numero :'806UAB51251'})
.then(res => {
console.log(res.data.firstname);
})
.catch(function (error) {
console.log(error);
});
当我console.log(res.data);
给我
{"firstname":"Lilo","lastname":"CHAPE","section":"","number":"21299345"}
但是console.log(res.data.firstname);
在给undefined
。
来自php服务器的代码:
function displayCard($section, $firstname, $lastname, $number, $groupe) {
$data=array('firstname'=>$firstname,'lastname'=>$lastname,'groupe'=>$groupe,'section'=>$section,'number'=>$number);
echo json_encode($data, JSON_PRETTY_PRINT);
...
}
答案 0 :(得分:0)
所以问题确实出在json上,我将代码发布到其他地方,并在我" { "
json时注意到缺少字符output
,所以我这样做了:
var badJSON = response.data;
var obj = eval( '(' + badJSON + ')' );
console.log( obj ); // Logs: Object {a: "b"}
console.log( obj.firstname, obj.lastname );
问题已解决!