[英]ionic2: API response data undefined?
我正在使用ionic2
,我想向api服務器發出登錄請求,但是我有response => undefined
這是provider中的代碼:
loginUser(email, password, macId, token) {
let userObject = {
"email": email,
"password": password,
"mac" : macId,
"token" : token
}
var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
console.log('object: ',userObject);
return this.http.post(this.loginUrl, userObject, {headers:headers})
.map((response: Response) => response.json())
}
這是login page
的代碼:
loginUser(email,password) {
console.log('email: ',email,'password: ',password);
if(!this.loginForm.valid){
this.submitAttempt = true;
} else {
this.submitAttempt = false;
this.deviceInfo.macId = this.device.uuid;
this.fcm.getToken().then(token=>{
this.token = token;
});
this.userProvider.loginUser(email,password,this.deviceInfo.macId,this.token)
.subscribe(data=> {
alert("data is "+data);
},
(err)=>{
alert("err"+err);
}
)
}
輸出應為:
{
msg : "SyGgTmNHxJcZFYJu6RootUHAqzdkhPNzsTGJHipeBZQSN8nHdbHga4gQ3jENesNPsK5tdtGKlmUa5g3cIVDO4ZqqENd5uPizwgWkq6z3CyUXAhyns8PTnNPwax7lgKRiY7I67qmiPCpZdwu2Kh5v7U"
}
但實際輸出:
data: "undefined"
我該怎么辦?
您不需要對其進行map
,只需在userProvider
函數中返回this.http.post
return
的promise
,並在調用它的位置使用userProvider
.then()
即可獲取數據:
loginUser(email, password, macId, token) {
let userObject = {
"email": email,
"password": password,
"mac" : macId,
"token" : token
}
var headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
console.log('object: ',userObject);
return this.http.post(this.loginUrl, userObject, {headers:headers});
}
和...
loginUser(email,password) {
console.log('email: ',email,'password: ',password);
if(!this.loginForm.valid){
this.submitAttempt = true;
} else {
this.submitAttempt = false;
this.deviceInfo.macId = this.device.uuid;
this.fcm.getToken().then(token=>{
this.token = token;
});
this.userProvider.loginUser(email,password,this.deviceInfo.macId,this.token)
.then(data=> {
alert("data is "+data);
},
(err)=>{
alert("err"+err);
}
)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.