0

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:

  1. submit pressed
  2. success
  3. complete

However instead of success I get error in the Samsung A5:

  1. submit pressed
  2. error
  3. 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.

Rasul
  • 223
  • 1
  • 2
  • 12
  • Were you able to pinpoint the cause of the issue? I'm having a similar Ajax call issue on a Samsung Galaxy S9, Chrome. The code works fine on other devices I tested so far. – yenren Aug 17 '19 at 15:55
  • @Envayo: unfortunately I haven't found a solution...sorry! – Rasul Aug 19 '19 at 05:34
  • After my comment, the person who reported the issue told that after clearing the browser cache (he had reported before he had done it but...), and the script is working fine now. May not be the same issue as yours, but my experience has been that clearing browser cache solves about half of the reported issues. – yenren Aug 20 '19 at 05:42

2 Answers2

0

From this data, I cant figure it out. Please use below code to assume what actually happened. Use the following function to decode error and let me know that output, instead of simple 'error' message.

function formatErrorMessage(jqXHR, exception) {

if (jqXHR.status === 0) {
    return ('Not connected.\nPlease verify your network connection.[Error Code=0]');
}
else if (jqXHR.status == 400) {
    return ("Server understood the request but request content was invalid.[Error Code=400]");
}
else if (jqXHR.status == 401) {
    return ('Unauthorised access. [Error Code=401].');
}
else if (jqXHR.status == 403) {
    return ("Forbidden resouce can't be accessed.[Error Code=403]");
}
else if (jqXHR.status == 404) {
    return ('The requested page not found. [Error Code=404]');
} else if (jqXHR.status == 500) {
    return ('Internal Server Error [Error Code=500].');

}
else if (jqXHR.status == 503) {
    return ('Service Unavailable. [Error Code=503]');
}

else if (exception === 'parsererror') {
    return ('Requested JSON parse failed.');
} else if (exception === 'timeout') {
    return ('Time out error.');
} else if (exception === 'abort') {
    return ('Ajax request aborted.');
} else {
    return ('Uncaught Error.\n' + jqXHR.responseText);
}
}

Then change your ajax call's error handler like this,

   error: function(req, status, err){
        alert (formatErrorMessage(req, err));
    },

And let me know waht the output say in samsung set.

Ayan_84
  • 645
  • 1
  • 6
  • 18
  • thank you for detailed decoding of the error message. I had done this before in much simpler form of just getting the status which is "0" in case of the error and "200" in case of success, but had them removed when posting to simplify the code. Based on your description "0" means no connection which is equally puzzling: how do 5 other browsers make the connection and one fails to connect! Thanks again for your input and time. – Rasul Feb 12 '19 at 02:11
  • You should use Fiddler to set the proxy and easily analyze the result. May be a ssl certificate issue,Proxy issue, or other problem. But unless you debug the network connection, no concrete resolution will be reached. If you can't use fiddler, google it and its pretty easy. – Ayan_84 Feb 12 '19 at 03:21
  • You might be on something in regards to SSL or Proxy as I don't see anything else in this basic setup that should cause a different behavior from one device to another. I've not used Fiddler before, but I'll look into it and if I find anything worth sharing I'll update it here as it might be useful to somebody else with similar problem. – Rasul Feb 13 '19 at 02:58
0

Without knowlage of the php code behind this application I can tell you that you should return the status 200 to join the success function.
Also you should avoid caching in this case (at leats for development). And inspect the response status of your php script. Jquery doesn't fool you on randomly taking suucess or error functions, there is a reason in response handling.
For references you can have a look at jQuery docs.
You should remember the differences of mobile device browsers. Chrome does behave different as firefox if you are watching the js compilation and execution.

Also you can use tools like browserstack for testing different mobile devices in layout and js functionality.

Beside: In my opinion frontend devs (yes, js is frontend and CAN be also backend) always have to test on multiple devices/browsers or at least emulate them.

Spears
  • 2,102
  • 1
  • 17
  • 27
  • I can't agree with you more on testing and browserstack is one the best if not the best tool out there. But this is not rocket science in a simple jQuery call with a PHP code that can't do anything wrong (since it doesn't do anything at all and exits right away!) I will try sending back 200 instead of 0 from the PHP file to see if that will make any difference. Could you kindly expand on the "avoid caching" please. – Rasul Feb 12 '19 at 02:20
  • In my very personal opinion the best way to avoid the high testing effort is to automate your frontend/UX tests. Im using codeception for it. This way you dont have to care about the most common disfunctionalities and dont need to repeat every test step in your application. Caching (clientside; browser) can store your "deprecated" older version of JS in the cache. You should set a lifetime for the browsercache. Caching (serverside) depends on your framework (if you are using one) and your code. Some things might be stored in the php opcache. Even if I dont think its used in your case. – Spears Feb 12 '19 at 07:18
  • Small update: returning 200 instead of 0 from PHP didn't change anything and I don't think it should have anyway. – Rasul Feb 13 '19 at 02:53
  • @Rasul Could you give me some informations about the browsers used on your phone and the versions? It is quite possible that your problem is caused by chrome/firefoox/opera ... - versions – Spears Feb 13 '19 at 10:47
  • It's Chrome 72.0.3626.105 and Samsung Internet 8.2.01.2 (I have no idea whose browser they bundle with the phone under their name) on Android 8.0. I doubt it is the browser that causes this problem. – Rasul Feb 15 '19 at 05:03
  • @Rasul this migth be a shot in the dark but maybe this is caused by Android v 8.0 initial release: https://support.google.com/work/android/answer/7506908?hl=en .... – Spears Feb 15 '19 at 11:22