ionic2: API 响应数据未定义?
ionic2: API response data undefined?
我正在使用 ionic2
,我想向 api 服务器发出登录请求,但我有响应 => undefined
这是提供程序中的代码:
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
它,只需 return
this.http.post
在您的 userProvider
函数中返回的 promise
并使用 .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);
}
)
}
我正在使用 ionic2
,我想向 api 服务器发出登录请求,但我有响应 => undefined
这是提供程序中的代码:
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
它,只需 return
this.http.post
在您的 userProvider
函数中返回的 promise
并使用 .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);
}
)
}