Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework ionic2:API响应数据未定义?_Ionic Framework_Ionic2_Ionic2 Providers - Fatal编程技术网

Ionic framework ionic2:API响应数据未定义?

Ionic framework ionic2:API响应数据未定义?,ionic-framework,ionic2,ionic2-providers,Ionic Framework,Ionic2,Ionic2 Providers,我使用的是ionic2,我想向api服务器发出登录请求,但我有response=>undefined 这是提供程序中的代码: loginUser(email, password, macId, token) { let userObject = { "email": email, "password": password, "mac" : macId, "token" : token } var headers = ne

我使用的是ionic2,我想向api服务器发出登录请求,但我有response=>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())    
  }
这是登录页面中的代码:

输出应为:

{
  msg : "SyGgTmNHxJcZFYJu6RootUHAqzdkhPNzsTGJHipeBZQSN8nHdbHga4gQ3jENesNPsK5tdtGKlmUa5g3cIVDO4ZqqENd5uPizwgWkq6z3CyUXAhyns8PTnNPwax7lgKRiY7I67qmiPCpZdwu2Kh5v7U"
}
但实际产出:

data: "undefined"
我该怎么办?

您不需要映射它,只需在您的userProvider函数中返回this.http.post返回的承诺并使用即可。然后在何处调用它以获取数据:

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);
       }
     )
}

因此,您得到的错误输出发生在这里:this.userProvider.loginUseremail,password,this.deviceInfo.macId,this.token.subscribedata=>{alertdata is+data;},?您的this.loginUrl?@ewizard log response.json是从logiuser函数中获取的,并查看它是否在那里未定义……也不是返回this.http.posthis.loginUrl,userObject,{headers:headers}.mapresponse:Response=>Response.json尝试返回this.http.postthis.loginUrl,userObject,{headers:headers}不带映射,而不是。使用userProvider订阅…使用.thendata=>{…subscribe是用于可观察的,我很确定this.http.post会返回一个使用then而不是subscribebeit的承诺。谢谢你,我删除了map并使用subscribe而不是then,因为我在then>>中出错,它没有被清除,这是什么问题!
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);
       }
     )
}