4

I have implemented a simple AJAX post (through JS XmlHttpRequest object) to a web page in webserver.

However, I observed a weird symptom/issue on default browser in Android OS 2.X That is, if for some reason there is long enough delay for the webserver to come back with a response (can be simulated by hard coding Thread.Sleep in the web page), the browser would automatically re-post the same request to the webserver again during the wait.

<script type="text/javascript">
     $(document).ready(function() {
        PerformRequest();
    });

    function PerformRequest() {
        appendMessage('PerformRequest... ');

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", "http://localhost/DelaySite/Default.aspx", true);
        xmlhttp.onreadystatechange = function()
            {
            appendMessage("xmlhttp.readyState: " + xmlhttp.readyState, true);
            appendMessage("xmlhttp.status: " + xmlhttp.status, true);
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                // successful response
                }
            }
        xmlhttp.send();
    }
</script>

Has anyone seen this issue in Android browser? If so, does anyone know the cause?

Isn't this to be considered as a security issue? Consider case where each request can be possibly made twice if there is long enough network delay between mobile and the host.

However, I can't replicate this issue in ICS, only in Android OS 2.X

Regards,
Oliver

1

2 Answers 2

2

I encountered the same issue, however I discovered that it wasn't specific to the native browser (tried with Skyfire aswell). On these three Android phones the delay before the repeated request was (consistently):

  • Samsung Galaxy S2: 30 sec
  • HTC Desire: 60 sec
  • HTC Sensation: 60 sec

I asked around at work for advice and the response I got was to make the request asynchronous:

Make the server handle the request async and respond to the POST request immediately with a 201 including a location pointing to a status page / url indicating the progress of the POST request. The client can retrieve the progress of the request with a GET to this url.

0

can you put a console.log in the document.ready function to make sure it's not THAT that's getting fired twice? sometimes plugins etc can cause the document.ready to be fired twice.

maybe try calling the function in the ondeviceready event, as describe here: http://docs.phonegap.com/en/1.4.1/phonegap_events_events.md.html

1
  • I have confirmed that it's not document.ready that gets fired twice. Also I'm not testing a hybrid android app, just purely a html page running AJAX call. This double posting issue only occurs if host response comes back with a certain delay. Mar 5, 2012 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.