I am having a problem doing a jQuery AJAX POST in an Android application using Phonegap Build. I have added:
I have looked high and low for an answer to this. I have gone through several posts giving conflicting information, and I have tried a lot of the solutions mentioned, but none of them worked.
Here is what I am attempting to do:
$.ajax({
type: "POST",
url: "http://mydomain.com/mypage.aspx/myweb...",
data: "{'sEnquiryText':'" + $("#textareaEnq")[0].value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) { // Response function
if (data.d == "Success")
$("#lblInfoMsg").html("Enquiry sent successfully !");
else
$("#lblInfoMsg").html(data.d);
}
});
This works fine as a web application when installed on a different domain (myseconddomain.com/mywebapp) as well as local IIS (localhost/mywebapp). But it does not work on an Android app made using Phonegap Build.
If someone could please look into this issue, I would greatly appreciate it.
You need add to "res/xml/config.xml" some like
<access origin="http://127.0.0.1*"/> <!-- allow local pages -->
<access origin="https://example.com" />
<access origin="https://example.com" subdomains="true" />
<access origin=".*"/>
The answer, I've concluded after many hours, is that XHReq POST is not supported inside an Android WebView, at least in the default setup by PhoneGap on the Android 4.03 device I'm using; tcpdump shows that the request does come out of the device, for the correct host and path, but as a GET, with no post data. I checked the whitelist (phonegap default currently "*", so not an issue), made sure this was not a "POST caching" issue (by adding ?t= in the request url), and made sure I was making only asynchronous requests. Same result; POST comes out as a GET, and all post data is lost.
My solution has been to switch to using GET for this function, to avoid needing to send post data, via urls like "http://mydomain.com/endpoint?post-data=url-encoded-data-here&t=timestamp-here". This was possible only as I had control over the server end of it as well, changing the endpoint to allow the formerly POST-only operation over GET if the data is inlined this way.
UPDATE: I came back to this issue as I really wanted POST to work for other reasons (see post #21192670).
The reason why POSTs were failing was because of the MX configuration of my domain, and choice (or lack of) sub-domain in referencing it; I used the root domain in the POST url, but the MX record had this directing to wwww.mydomain.com, which was then pointed to the correct ip address. The POST was not getting through that redirect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With