Not able to make AJAX call in Android


#1

Hi All

I am having a strange problem. I am not able to make Ajax call from the application in Android. However it works very well with normal browser setting. I have made the proper CORS setting at server.

I am getting 403 Forbidden . And it seems something is getting blocked in webview itself. Nothing is going to server.

One more thing that I noticed is that in Request Header , Origin is file:// .

Am I missing something basic ?

Regards
Ankit


#2

Have you checked that all of the needed permissions are included in the manifest?


#3

Yes all permission are fine


#4

This seems to be the CORS filter issue in TOmcat

See thread

But still question comes why Android is sending the invalid URI. Do I have mis-configured my cordova config.xml ?

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.ionicframework.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Medistreet</name>
    <description>
        An Ionic Framework and Cordova project.
    </description>
    <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <preference name="fullscreen" value="true" />
    <preference name="webviewbounce" value="false" />
    <preference name="UIWebViewBounce" value="false" />
    <preference name="DisallowOverscroll" value="true" />
    <feature name="StatusBar">
      <param name="ios-package" value="CDVStatusBar" onload="true" />
    </feature>
	
</widget>

#5

Cordova Version :3.3.1-0.4.2


#6

I have the same isse but I do not understand the solution? I need to upgrade something? @ankisinghal @mquadrat ?


#7

HI

Are you using Tomcat as backend? If yes you might need to change the CORS filter for same.

For detailed discussion

http://mail-archives.apache.org/mod_mbox/tomcat-users/201404.mbox/browser

Move to 3rd page and look for CORS issue discussion


#8

I had the same problem and I solved it by turning off the CORS filter (by not using the CORS filter) on Tomcat.
Here is the detail.

(1) If you installed an app using “$ ionic run android” command, turn off the CORS filter on Tomcat.

Android Webview can access any site and it does not have a CORS issue like a regular Chrome browser.
For the same reason, you should not use the CORS filter on your production Tomcat server for the android mobile app accessing your site.

(2) If you installed an app using “$ionic run --livereload android” command during development, turn on the CORS filter on Tomcat.

When using ‘–livereload’ option, the app’s ‘Origin’ HTTP header will be sent with something like “http://localhost:9100”. When Tomcat sees this Origin header with “http://”, it will consider it as CORS access from a browser. So, the CORS filter needs to be turned on on Tomcat. In this case, if the CORS filter is not set up on Tomcat, the access will not be allowed.


#9

I am running tcpdump and the Android emulator is not even making a network request at all. I have configured CORS correctly on the server-side, as I had to fix it for the iOS emulator – which works fine now.

But when I launch ionic run android any AJAX request just immediately returns an error and tcpdump shows no traffic was sent to the server.

AndroidManifest.xml has:

<uses-permision android:name="android.permission.INTERNET" />

Cordova config.xml has:

<access origin="*"/>

Normally with a CORS issue the browser will at least issue an HTTP OPTIONS request. The Android emulator isn’t sending any traffic when I launch the app.


#10

This is exactly my problem too. My app works fine using ‘ionic serve’ and ‘ionic emulate’, but as soon as I try it native on my Nexus 5 or Ionic View, I get a 403 Forbidden on my $http call.

I do have CORS setup on the backend.


#11

I am in the same situation. Have you made any progress? I am able to make http and https calls from my devices (Android and iPhone), however my call to Amazon AWS using Javascript for Browser is getting a 403 Forbidden. Running the same code using ‘ionic serve’ works fine.


#12

This worked for me (after 3 hours of head banging, I stumbled across this post)…