I have encountered a peculiar problem using jQuery (3.3) ajax. I have simplified the code to bare minimum and still can't understand why it is happening.
From an HTML form I capture the 'Submit' and call a PHP script using ajax. The ajax call is successful if I use Chrome or Edge on a (Win10) laptop or Chrome on an older Samsung Note II (running Android 4.x), or Safari on iPhone, or two other Android phones, but it fails (hits the error clause) using either Chrome or Samsung stock browser on a Samsung A5 phone. All the HTML/JavaScript/PHP are on the same server and same domain so no cross domain scripting.
JavaScript:
$(document).on("submit", function(ev) {
alert ('submit pressed');
ev.preventDefault();
var destination = encodeURI ("https://example.com/contact.php");
var datastring = "test";
$.ajax({
type: 'POST',
url: destination,
data: datastring,
success: function (result) {
alert ('success');
},
error: function(req, status, err){
alert ('error');
},
complete: function(result){
alert ('complete');
}
});
});
PHP - I have taken everything out in the cotnact.php:
<? php
exit(0)
?>
I get 3 alerts if I use laptop, iPhone, or 3 other Android phones:
- submit pressed
- success
- complete
However instead of success I get error in the Samsung A5:
- submit pressed
- error
- complete
By the way, although the call in the A5 phone hits the "error" clause, the PHP code is indeed gets executed (I had the whole script working by writing to files, sending email notifications, ... but removed everything to simplify)
Does anybody have any idea why this is happening and more importantly how can anybody be sure that his/her code is working on any device when the code is at a such basic level without testing every and each device?
Thank you in advance if you can shed any light on this and point me to what I am missing in either my code, setup, or understanding.