0
我有這個JSON文件,我試圖解析第二個關卡。但它似乎失敗了。用Ionic讀取單個第二級JSON條目
{
"error":false,
"accessToken":"xxx",
"accountId":"xxx",
"account":[
{
"error":false,
"id":"2",,
"username":"Username"
}]
}
這裏的離子碼
return $http.post(SERVER.apiUrl + SERVER.apiLogin,
{username: "username",password: "password"})
.success(function(data){
o.setSession(data.accountId, data.accessToken, data.account[0]["username"]);
});
錯誤
TypeError: Cannot read property '0' of undefined
at services.js:103
at ionic.bundle.js:17151
at processQueue (ionic.bundle.js:20962)
at ionic.bundle.js:20978
at Scope.$eval (ionic.bundle.js:22178)
at Scope.$digest (ionic.bundle.js:21994)
at Scope.$apply (ionic.bundle.js:22282)
at done (ionic.bundle.js:17439)
at completeRequest (ionic.bundle.js:17629)
at XMLHttpRequest.requestLoaded (ionic.bundle.js:17570)
我也試圖與
data.account.username
和
data.account[0].username
有人可以提供一個快速建議嗎?
問候
我想這是因爲我需要它首先保存爲一個變量,但是這似乎並不在下面的代碼是可能的後 return $ http.post(SERVER.url + SERVER.apiLogin,{username:「username」, password:「Password」 })。success(function(data){ userName = data.account [0] [ 「用戶名」] o.se tSession(data.accessToken,userName,data.account.fullname,「success:ok」); }) –
你解析它嗎?/ data = JSON.parse(data); – Blaztix