3

I've been working on an Android mobile application which is a cross platform browser based application developed using HTML5, javascript, jQuery and PhoenGap.

Application being a client-server based, we make frequent asynchronous web service calls to fetch data from the server. In this scenario, we are facing problem while fetching data which involves huge amount of data. Here, in this case there are so many database queries that will run at the background to get the data.

We are testing the application using a 2G connection which is resulting in a status 0 error message after few mins.

My question here is, will there be an option for the mobile browser to abort a web service call after few mins before receiving the response?

We are making an ajax call to fetch the data. Sample code for your reference. We have tested with various timeout options but there is no positive result. The same is working fine on a 3G network.

$.ajax({
            type: "POST",
            url: wsUrl,
            contentType: "text/xml",
            dataType: "xml",
            data: soapRequest,
            async: true,
            timeout:300000,
            tryCount:0,
            retryLimit:3,
            success: processSuccess,
            error: processError
    });
4

1 回答 1

0

是的,您可以在 ajax 调用中使用超时。例如,这是:

  $.ajax({
            url: syncURL,
            data: {},
            dataType:"jsonp",
            timeout: 10000,
            jsonpCallback: 'jsonpxchange',
            success:function (data) {
                console.log("OK!!");
            },
            error: function(jqXHR, exception) {
                if (jqXHR.status === 0) {
                    alert('No network');
                } else if (jqXHR.status == 404) {
                    alert('Not fount [Error: 400].');
                } else if (jqXHR.status == 500) {
                    alert('[Error: 500].');
                }
                if (exception === 'parsererror') {
                    alert('parser error');
                } else if (exception === 'timeout') {
                    alert('timeout');
                } else if (exception === 'abort') {
                    alert('abort');
                } else {
                    alert('Undefined');
                }
        }
        });

如您所见,我使用timeout(以毫秒为单位),例如10secs. 然后开枪exception===timeout

于 2013-02-14T23:15:20.017 回答