Android Browser Issue with jQuery Ajax and when appcache is used
Asked Answered
S

2

6

we are having a problem with the built in browser on Android 4.0, 4.1 and 4.2 (we haven't got anything lower to test on). The problem is that the ajax call will work perfectly on first load, you can press the run Ajax button as many times as you like and it will be fine. You can disconnect from the internet and it will work properly. But if you exit (FULLY, make sure its not just running in the background) the browser then relaunch it, it will fail on load and on button press. It doesn't matter if you are on-line or off-line. The error that is been returned from the ajax call is "Error" with status = 0 and readyState = 0.
When its successful you get a message back says "respose from Ajax Call" with a status = 200 and a readyState = 4.

The code works find on every other browser we have tested on Android Chrome, Firefox and Opera. on IOS 5 and 6 it works and every desktop browser we can find. Is there something that I missing or have we found a bug in the built in browser. Any help on this would be appreciate especially if it just something stupid I have done.

We have created a test script that demonstrates this problem well I have attached it to the bottom of this message.

Thanks

Tim

test.php

<?php     
function displayPage() { 
?>
<!DOCTYPE html>
<html manifest="test.manifest" debug="true">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1" >
        <title>test</title>
        <script type="text/javascript" src="jquery-1.9.0.min.js"></script>
        <script type="text/javascript">
            function runAjaxGet() {
                var XMLHttpRequest = $.ajax({
                    url: "test.php",
                    dataType: "json",
                    data: "test=test",
                    traditional: "true",
                    success: function( responseData ) {
                        alert('good\n responseData: '+responseData+ '\n res:' + XMLHttpRequest.responseText +'\n readyState: '+ XMLHttpRequest.readyState + '\n Status: '+XMLHttpRequest.status );
                        console.log(XMLHttpRequest);
                   },
                   error: function (xhr, ajaxOptions, thrownError, responseData) {  
                       alert('bad\n responseData: '+responseData+ '\n res:' + XMLHttpRequest.responseText +'\n readyState: '+ XMLHttpRequest.readyState + '\n Status: '+XMLHttpRequest.status);
                       console.log(XMLHttpRequest);
                   }
                });
            }
            $(document).ready(function() { 
                runAjaxGet();
            });

        </script>
    </head>
    <body>
        <button Name="Run Ajax" onclick="runAjaxGet();">Run Ajax</button>
    </body>
</html>
<?php
}

function processRequests() {
    header("Content-Type: application/json; charset=UTF-8" );
    echo (json_encode("respose from Ajax Call"));
}

date_default_timezone_set ( "UTC" );

if (isset($_REQUEST['test'])) {
    $which = $_REQUEST['test'];
} else {
    $which = '';
}

switch ($which) {
    case "test":
        processRequests();
        break;

    default :
        displayPage();
        break;
}
?>

test.manifest

 CACHE MANIFEST
 test.php
 jquery-1.9.0.min.js
 test.php?test=test
Selfliquidating answered 25/1, 2013 at 5:18 Comment(0)
B
0

Just add NETWORK section with asterisk and it will work

 CACHE MANIFEST
 test.php
 jquery-1.9.0.min.js
 test.php?test=test
 NETWORK:
 *
Breakfront answered 31/3, 2013 at 19:37 Comment(0)
H
0

I hit this same problem and determined that when retrieved from the cache, 0 indicates success. This is likely because there is no actual http request involved since the request is resolved entirely locally.

Appcache manifest file:

CACHE MANIFEST

CACHE:
/config

Javascript:

var request = new XMLHttpRequest();
request.open('GET', '/config', false); // async=false is ok because this file will always come from AppCache
request.send(null);

// Older versions of android return 0 when ajax request retrieved from appcache
if (request.status == 200 || request.status == 0) {
  return JSON.parse(request.responseText);
} else {
  console.log("ERROR: config not retrievable");
  throw "Attempt to retrieve config return http status " + request.status;
}
Hormuz answered 12/2, 2015 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.