Local Ajax request / Sencha / PhoneGap on Android

白昼怎懂夜的黑 提交于 2019-12-11 04:49:40

问题


I am trying to load some content in a PhoneGap App via a local Ajax request. It seems to work fine on iPhone, but Android wont find the file.

It goes like this:

Ext.Ajax.request({
        url: 'file:///android_asset/webapp/content/file.html',
        success: function(response, opts) {
            console.log('response ' + response.responseText);
        }
        failure: function(response, opts) {
            console.log('response ' + response.responseText);
        }
    });

I already adjusted the Manifest and did set the Internet Permission, still no success.

Do you have any idea?


回答1:


The first thing I do in situations like this is to get rid of the dependency on third party libraries like Sencha or jQuery and go straight to use XHR. There is an issue where making requests from file protocol sometimes returns a status of 0 when it actually is 200. Most libraries have this fixed but you never know. Try this XHR code which should work just great on Android:

var request = new XMLHttpRequest();
    request.open("GET", "file:///android_asset/webapp/content/file.html", true);
    request.onreadystatechange = function(){
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                console.log("response " + request.responseText);
            }
        }
    }
    request.send();



回答2:


I'm not sure this totally meets your requirements but have a look at this Phonegap Android plugin collection. It includes plugins for uploading and downloading content for PhoneGap on Android.




回答3:


Which version of Sencha Touch are you using? As Simon pointed out, many libs report a failure when encountering a response code of 0, Sencha Touch fixed this as of 2.0.0

http://www.sencha.com/forum/showthread.php?173577-Ext.data.Connection-fails-when-evaluating-a-file-protocol-s-status

Second, try removing the file:///android_asset/webapp/ part of the ULR, a relative url to your content directory should work just fine, and alleviate any OS detection switching you might be doing to arrive at that path.

Ext.Ajax.request({
    url: 'content/file.html',
    success: function(response, opts) {
        console.log('response ' + response.responseText);
    }
    failure: function(response, opts) {
        console.log('response ' + response.responseText);
    }
});


来源:https://stackoverflow.com/questions/10813013/local-ajax-request-sencha-phonegap-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!