1

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

Grifflet
  • 31
  • 3
  • any errors in browser console ? – Parth Ghiya Feb 01 '17 at 09:00
  • 1
    Possible duplicate of [AJAX Call not working in Phonegap but working normally](http://stackoverflow.com/questions/20717128/ajax-call-not-working-in-phonegap-but-working-normally) – Akshay Tilekar Feb 01 '17 at 09:01
  • no the only error I get in the browser console is when my mobile is link and I am trying to post json data to the server, it just returns a 404 error – Grifflet Feb 01 '17 at 09:40
  • it depends on what happens when the online builder does to app. Have you tried to put an absolute url? like http://localhost:8888 and so on. – misterwolf Feb 01 '17 at 11:10

1 Answers1

2

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.

Grifflet
  • 31
  • 3