Search code examples
androidionic-frameworkionic2ionic-view

ionic 2: while run in emulator it shows error but running in server it works


something really weird happened me after i just add some plugins in my app.. when i run

ionic run serve

the app work find and i can get response from server as you can see here enter image description here

but where i run

ionic run android

it show the spinner and shows error as you can see here

enter image description here


Solution

  • To do http requests via an ionic app, you need to white list the urls. You can use cordova whitelist plugin to do this.

    cordova plugin add cordova-plugin-whitelist

    and in the index file

    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-eval' 'unsafe-inline' *; object-src 'self'; style-src 'self' 'unsafe-inline'; media-src *">

    this code was copied by the following tutorial, have a look at it.

    http://www.gajotres.net/ionic-2-making-rest-http-requests-like-a-pro/

    HTH