Tuesday, January 03, 2017

Ajax call not working as expected in Firefox



Ajax call not working as expected in Firefox

Okay, I had this peculiar problem of my web form updating the relevant tables when being called from Google Chrome and not doing so, when the call was made through Firefox.

The form went into sleep and did not respond after the Submit button was clicked. It looked as if the Ajax call was in indefinite loop in Mozilla Firefox.

As we went digging for reasons that this was happening, we realised that Firefox is strict about how data has to be sent to it.

Google Chrome is someone who tries to make sense out of the data and so, we got away with Chrome.

So, we understand that what we need to return from a php, which is being called through an ajax request, is content preceded by a header, which indicates the type of content that follows.

It could be any of these types as defined below.

Header Content-type: Common examples:
application/json
application/x-www-form-urlencoded
application/pdf
multipart/form-data
text/html
text/plain
image/png
image/jpeg

image/gif

So, at the end of your php code, you will  have something like what you are seeing below. In the first example, the returned value is a json content, while the second example returns a plain text message in response to the Ajax request.

Php program returning JSON in response to an Ajax call, Example 1:


    header('Content-type: application/json');
    echo json_encode($response_array);
?>


Php program returning text value to Ajax call, Example 2:


    header('Content-type: text/plain');
    echo 'success';
?>

No comments:

Nature, most benign, most benevolent!

We have this habit of blaming nature for all our ills. Obviously, because man is the most intelligent creature on Earth and cannot go wro...

Copyrighted.com Registered & Protected DWYE-NHTO-NBNH-7FFM