Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova app is getting 404 for all XMLHttpRequests on Android

This is a common issue for people to have but all of the solutions on StackOverflow seem to center around installing cordova-plugin-whitelist, adding some variation on

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

to config.xml, and adding something like

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

to your index.html file. I've tried all of these things but still no luck. What's really frustrating is I had worked on a previous project where these things did work as a solution but using the same configuration on the new project just doesn't work.

I'm 100% sure that the requested URL is responding properly. Even when I click on the URL from the 404 error in the Chrome Dev Inspector it returns the proper content. Is there anything to try not mentioned in the same answers that you see over and over when you search for this question?

Edit:

In response to the first comment, this is the javascript code:

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://example.com/endpoint");
xhr.onload = success_callback;
xhr.onerror = error_callback;
xhr.send(content);

This exact same code works fine when I load it in a desktop browser, the 404 only occurs in the cordova app. There's no difference in the code, it's just me running cordova build browser vs me running cordova build android.

like image 310
Ivanna Avatar asked Dec 18 '22 21:12

Ivanna


1 Answers

One time this happened with me.

List all installed plugins: cordova plugin list

All I had to do was remove the plugin: cordova plugin remove <PLUGIN_NAME> (cordova-plugin-whitelist in that case)

Then add it again: cordova plugin add cordova-plugin-whitelist

like image 172
user1027620 Avatar answered Dec 28 '22 05:12

user1027620