javascript - Phonegap AJAX request undefined -


i have problems cordova/phonegap/ajax requests webpage. since app working phonegap developer-app running on phone , sends ajax requests perfectly. think has permissions/plugins or something. when install app using cordova doesn't send , whole ajax request returns:

readystate: 0 responsetext: undefined status: 0 text status: error error 

in config.xml i've set

<access origin="*" /> 

and in androidmanifest.xml i'v set

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

here's ajax request itself

$.ajax({     method: "get",     crossdomain: true,     datatype: 'json',     url: 'http://mywebsite.com/projectname/index.php',     data: { x: userlocation.latitude, y: userlocation.longitude },     success: function(data){           alert("success: "+ data);     },     error: function(xhr, textstatus, err) {          alert("readystate: " + xhr.readystate);         alert("responsetext: "+ xhr.responsetext);         alert("status: " + xhr.status);         alert("text status: " + textstatus);         alert("error: " + err);     } }); 

including cordova.js project:

<script type="text/javascript" src="cordova.js"></script> <script src='js/jquery.js'></script> <script>     $(document).bind('mobileinit', function () {         $.mobile.changepage.defaults.changehash = false;         $.mobile.hashlisteningenabled = false;         $.mobile.pushstateenabled = false;     }); </script>  <script ...here comes js file ajax called out 

setting these didn't work either

$.support.cors = true; $.mobile.allowcrossdomainpages = true; 

if running cordova 5 or newer, need content security policy meta tag in html in order make ajax requests external servers. if started older cordova version , upgraded 5 or 6, index.html didn't have 1 of these in it. if started new cordova 5 or 6 app cli template "cordova ready" app have one, sample 1 provided doesn't allow ajax requests other servers unless explicitly configure it.

you add index.html allow ajax requests anywhere:

<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://api.fixer.io"> 

alternatively @ how configure connect-src @ content-security-policy.com or blog post here configure tighter csp suits needs.

additionally may want use cordova "deviceready" event rather "mobileinit", may making ajax call before cordova ready, ajax call in (ondeviceready) callback of:

document.addeventlistener('deviceready', this.ondeviceready, false); 

or in executes after has been called, indicating cordova ready.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -