Ionic 3 Response with status: 0 for URL: null

前端 未结 2 1988
庸人自扰
庸人自扰 2020-12-20 07:10

I have an Ionic 3 app. I recently added the iOS platform.

When i run it on iOS (emulator and device) all the server requests that has headers fail with the error

2条回答
  •  没有蜡笔的小新
    2020-12-20 07:57

    To avoid CORS problem specially in iOS you must use @ionic-native/http plugin which is actually Advanced HTTP plugin for API calling.

    Follow below steps to use this plugin

    Step 1: Add Http native plugin

    $ ionic cordova plugin add cordova-plugin-advanced-http
    $ npm install --save @ionic-native/http
    

    Installation Link : HTTP

    Step 2: Import HTTP native plugin in your file where you wants to cal API.

    import { HTTP, HTTPResponse } from '@ionic-native/http';
    

    Step 3: How to use this plugin for API call ?

    constructor(public httpPlugin: HTTP) {
    
      }
    
    //Set header like this
    this.httpPlugin.setHeader("content-type", "application/json");
    
    //Call API 
    this.httpPlugin.get(this.url, {}, {}).then((response) => {
        //Got your server response
    }).catch(error => {
        //Got error 
    });
    

    Hope this will help you.

提交回复
热议问题