带有api的Ionic应用未在真实设备上运行

时间:2018-11-14 08:59:18

标签: ionic3 slim

我用api开发了一个离子应用程序,我在Chrome中添加了allow-control-allow-origin,因此它可以在浏览器中完美运行,但是在真实设备中我的应用程序无法运行。我认为api没有被我的应用读取。它显示我的吐司“请检查您的网络连接”有人帮助​​我。对不起我的英语不好 这是我的代码

    let apiUrl = "http://mywebsite.com/api/public/api/";

    @Injectable()
    export class AuthServiceProvider {
      responseData:any;

    constructor(public http: Http) {
     console.log('Hello AuthServiceProvider Provider');
    }

    postData(credentials, type){
     return new Promise((resolve, reject)=>{
        let headers = new Headers();
        this.http.post(apiUrl+type, JSON.stringify(credentials), {headers}).subscribe(res=>{
            resolve(res.json());
        }, (err)=>{
            reject(err);
        });
     });
     }
  }

和我的login.ts

login(){
let loading = this.loadingCtrl.create({
    spinner: 'crescent',
  content: 'Please wait...'
});
loading.present();
this.authService.postData(this.userData,'login').then((result) => {
this.responseData = result;
if(this.responseData.userData){
  console.log(this.responseData);
  localStorage.setItem('userData', JSON.stringify(this.responseData));
  loading.dismiss();
  this.navCtrl.push(TabsPage);
  }
  else{
    loading.dismiss();
    loading.onDidDismiss(() => {
        const toast = this.toastCtrl.create({
          message: 'Invalid Email or Password!',
          duration: 3000
        });
        toast.present();
    }); 
  }
}, (err) => {
  loading.dismiss();
  loading.onDidDismiss(() => {
        const toast = this.toastCtrl.create({
      message: 'Please Check Your Internet Connection',
      duration: 5000
    });
    toast.present();
  });   
});
}

0 个答案:

没有答案