Reputation:
I'm trying to send an HTTP GET request and everything works great while I'm testing. But when I build the application and try to send request, I'm getting 404 error.
Here is my index.html
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<access origin="*" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://google.com">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>PhoneGap</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
var start = Date.now();
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if(xmlhttp.status == 200){
//alert(Date.now() - start);
alert("yeah");
}
else if(xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert("else: " + xmlhttp.status);
}
}
}
xmlhttp.open("GET", "http://google.com", true);
xmlhttp.send();
</script>
</body>
I'm new to PhoneGap and I'm sure this is as simple as it gets, but I can't find anything on google.
Thanks in advance
Upvotes: 1
Views: 553
Reputation: 327
Here I have assumed that you are using Phonegap version "cli-5.2.0" and facing issue on Android. To fix 404 issue, add following lines in your config.xml file
<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />
Whitelist plugin is required to use with Phonegap "cli-5.2.0" to successfully make request to any server. Link for whitelist plugin https://github.com/apache/cordova-plugin-whitelist
Upvotes: 0
Reputation: 4148
It is likely that you need to set a Content Security Policy meta tag in your index.html as Cordova / PhoneGap 5 or higher will require this.
An example would look like this (in the head section of index.html):
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://myserver">
Swapping http://myserver for the server you're trying to access. You may actually be getting error messages such as this in the JS Console if this is your problem:
Refused to connect to ‘http://myserver/ping' because it violates the following Content Security Policy directive: “default-src ‘self’ data: gap: https://ssl.gstatic.com ‘unsafe-eval’”. Note that ‘connect-src’ was not explicitly set, so ‘default-src’ is used as a fallback.
SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
This post may help with understanding Content Security Policy.
Upvotes: 1