离子框架错误 - 响应状态:0表示URL:null

时间:2017-06-07 09:37:48

标签: angular ionic-framework

我正在研究离子应用程序。在服务器端,有CodeIgniter。存在CORS问题,因为登录不起作用。 在浏览器中它工作正常,但在模拟器和设备中有这个错误 -

状态响应:URL为0:null

搜索,尝试了很多,但找不到可行的解决方案。请检查以下代码并帮助我解决问题。

ionic.config.json

  "proxies": [
    {
      "path": "/login",
      "proxyUrl": "http://www.mydomain.coo/members/login"
    },
    {
      "path": "/update",
      "proxyUrl": "http://www.mydomain.coo/members/update"
    },
    {
      "path": "/register",
      "proxyUrl": "http://www.mydomain.coo/members/register"
    },

login.ts

  login() {
    this.showLoader('Authenticating...');
    this.postData = { isApp: '1', key: 'login', email:this.email, password:this.password };

    let data = JSON.stringify(this.postData);

    this.auth.login( this.postData ).then((result) => {

         this.loading.dismiss();

         this.data = result;
         if ( this.data.status == true )
         {
             localStorage.setItem('id', this.data.id);

    this.navCtrl.setRoot(TabsPage);
          }
          else
          {
            this.showLoader('Invalid Credentials...');
            this.loading.present();
            setTimeout(() => {
              this.loading.dismiss();
            }, 2000);
          }

    }, (err) => {
     this.loading.dismiss();
     this.presentToast(err);
    });


  }

AUTH-service.ts

  login(postData) {

    return new Promise((resolve, reject) => {

      let headers = new Headers( {
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
        'Content-Type': 'application/json; charset=UTF-8',
        'Accept': 'application/json'
      });

        this.http.post('/login', JSON.stringify(postData), {headers: headers})

        .subscribe(res => {

          resolve(res.json());

        }, (err) => {
          reject(err);
      });



    });


  }

Codeigniter index.php文件我有这个

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: X-PINGOTHER, Content-Type");      
header("Access-Control-Max-Age: 86400");

2 个答案:

答案 0 :(得分:0)

使用

替换auth-service.ts中的代码
let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
来自您的auth-service.ts

 let headers = new Headers( {
    'Access-Control-Allow-Origin' : '*',
    'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
    'Content-Type': 'application/json; charset=UTF-8',
    'Accept': 'application/json'
  });

答案 1 :(得分:0)

第 1 步: 把它放在你的 php 代码之上 header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: *');

第 2 步: 创建一个 .htaccess 文件并粘贴下面的代码 <IfModule mod_headers.c> Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Headers "*" </IfModule>

快乐编码,享受