最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cordova app is getting 404 for all XMLHttpRequests on Android - Stack Overflow

programmeradmin3浏览0评论

This is a mon 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 ment, this is the javascript code:

var xhr = new XMLHttpRequest();
xhr.open("POST", "");
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.

This is a mon 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 ment, this is the javascript code:

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://example./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.

Share Improve this question edited Dec 4, 2015 at 3:00 Ivanna asked Dec 4, 2015 at 2:23 IvannaIvanna 1,2471 gold badge14 silver badges26 bronze badges 5
  • What's your code look like? What's the URL? Can the Android device / simulator etc resolve that URL? Do you get anything of interest in the JS console in the Chrome remote inspector? – Simon Prickett Commented Dec 4, 2015 at 2:27
  • The code is a pretty standard usage of XMLHtppRequest, I'm fairly experienced in using it and I really don't think that it's where the issue lies. Additionally, the code works fine when I just load it in the browser instead of running it within an app.I get the same behavior on the emulator that I do on real devices. The 404 error es up in the Chrome remote inspector when the request is made and includes the link that returned 404 as a link in the inspector. When I click on the link it works as expected in the browser. – Ivanna Commented Dec 4, 2015 at 2:53
  • One time this happened with me. Removed and reinstalled the plugin and it suddenly worked. Don't know if it might help in your case tho. – user1027620 Commented Dec 4, 2015 at 3:02
  • Thanks, this worked! No clue why I needed to do that this time but I'm extremely grateful for a solution. – Ivanna Commented Dec 4, 2015 at 3:54
  • @user1027620, if you post that as an answer then I'll be happy to accept it because it was the right thing to do in this case. – Ivanna Commented Dec 4, 2015 at 5:34
Add a ment  | 

1 Answer 1

Reset to default 12

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

发布评论

评论列表(0)

  1. 暂无评论