简体   繁体   中英

Phonegap android ajax calls working in browser, not on mobile

I need some help with a phonagap/cordova app, its very basic but I cannot get my ajax calls to work of my mobile to the server, everything works fine in the browser. Please note I use phonegap's online builder to compile my code.

I have tried all of the following:

  • $.support.cors = true.
  • access origin="*"
  • allow-intent href="*"
  • allow-navigation href="*"

Now I have looked at whitelist but for some reason it breaks my config file when adding it, then I tried legacy whitelist, then I cannot get it built online on phonegap.

my ajax method:

$("#btnTest").click(function (evt) {
        var value = "";
        var options = {};
        options.url = "http://*/MobileService/ServiceFile.asmx/test";
        options.type = "POST";
        options.data = JSON.stringify({ "data": value });
        options.dataType = "json";
        options.crossDomain = true;
        options.contentType = "application/json";
        options.success = function (result) { $("#error-msg").text(result.d); };
        options.error = function (err) { $("#error-msg").text("Error saving the data!"); };
        console.log(value);
        $.ajax(options);
        evt.preventDefault();
    });

my index.html head tag:

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MobileApp</title>
<meta name="viewport" content="width=device-width, initial-scale=1">   
<link href="css/jquery.mobile-1.4.5.min.css" rel="stylesheet" />
<script src="scripts/jquery-2.2.0.min.js"></script>
<script type="text/javascript">
$(document).bind("mobileinit", function () {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
});
</script>
<script src="scripts/jquery.mobile-1.4.5.min.js"></script>
<script src="scripts/jSignature/jSignature.min.js"></script>
<link href="css/index.css" rel="stylesheet" />

Also note I am using visual studio 2013 to develop in.

Thanks in advance

So to answer my own question, in the end I found the legacy version of the the whitelist plugin.

As soon as I added this to my project it solved the CORS error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM