简体   繁体   中英

IonicApp angularjs $http post works on WIFI but not on certain mobile data

I have an ionic-angularjs app that does a post request to a Slim3 framework remote url ( http://xxx.cloudapp.net/xxx/api/v1/login/username/password ) but I always get the 404 status code as show in the screenshot

截图

The same happens even if I host the REST API in a different IP address.

I have checked all over and I have seen solutions like adding the cordova-whitelist plugin which I have done but still the error persist (even removing and adding the plugin again and also removing and adding the android platform ). I have even set the CORS.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, Content-Type,x-www-form-urlencoded, x-xsrf-token, X-Auth-Token , Authorization');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');      
header('Access-Control-Allow-Methods: POST, GET, DELETE, OPTIONS, PUT');        

The biggest dilemma is the app works very fine when I test using the chrome browser,Postman and also when the APK file in installed and the phone is on WIFI, it also works fine when I'm using mobile data from a different mobile provider except for one mobile provider that has this IP (172.22.2.38:8080).

Another weird thing is when I send the post request using the Advanced Rest API downloaded from play store when in the (172.22.2.38:8080) the request returns the 200 status code as long as I set the Body->Type to 'x-www-form-urlencoded'. So the problems seems to be when I use post request with angularjs $http.

This is the androidManifest.xml (part of it)

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

and config.xml looks like this (part of it)

<access origin="*" subdomains="true"/>
<allow-navigation href="*"/>

<plugin name="cordova-plugin-device" spec="~1.1.3"/>
<plugin name="cordova-plugin-console" spec="~1.0.4"/>
<plugin name="cordova-plugin-splashscreen" spec="~4.0.0"/>
<plugin name="cordova-plugin-statusbar" spec="~2.2.0"/>
<plugin name="ionic-plugin-keyboard" spec="~2.2.1"/>
<plugin name="cordova-plugin-whitelist" spec="https://github.com/apache/cordova-plugin-whitelist.git"/>
<plugin name="cordova-plugin-contacts" spec="~2.2.1"/>
<plugin name="cordova-plugin-camera" spec="~2.3.1"/>
<plugin name="cordova-plugin-media-capture" spec="~1.4.1"/>
<plugin name="cordova-plugin-file" spec="~4.3.1"/>
<plugin name="cordova-plugin-file-transfer" spec="~1.6.1"/>

I have been stuck for almost two weeks any help will be greatly appreciated.

The problem was with the proxy, I had to do this in virtual host conf (/etc/apache2/sites-available/000-default.conf ).

    ProxyRequests On
    ProxyVia On
    ProxyTimeout 60
    <Proxy "*">
            Order allow,deny
            Allow from all
    </Proxy>

This one works perfectly if I make a post request to a remote server.

Try this. In Config.xml, allow access & navigations to your domains:

<access origin="http://yourdomain1.com" />
<allow-navigation href="http://yourdomain1.com" />

Then in index.html, add the Content-Security-Policy as below:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' http://yourdomain1.com  data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline';  media-src *;  script-src 'self' 'unsafe-eval' 'unsafe-inline';">

Replace http://yourdomain1.com with your server url from where you are calling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM