使用PhoneGap的,不能在Android上使用Ajax加载本地.html文件只 [英] Using PhoneGap, can't load local .html file using Ajax on Android only

查看:352
本文介绍了使用PhoneGap的,不能在Android上使用Ajax加载本地.html文件只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap的(最新的),jQuery的1.7。我有麻烦得到一些HTML通过AJAX加载到一个div。我的问题很简单,我在WWW /目录中的index.html文件。和js文件,做这样的:

I'm using PhoneGap (latest), Jquery 1.7. I'm having troubles getting some html loaded via AJAX into a div. My problem is simple, I have an index.html file in the www/ directory. And a js file that does this:

$.ajax({
 type:"GET",
 timeout:10000,
 dataType: "html",
 async: false,
 cache: false,
 url: "file:///android_asset/www/_liqui-cult.html",
 success: function(data) {
  $('#data_details .description').html(data); // never runs
 },
 error: function(xhr,msg){
   alert(xhr.status + ", " + msg);
   if(xhr.status == 0 || xhr.status == "0"){
    alert(xhr.responseText); // always blank, if runs
   }
 }
});

在花了一天谷歌搜索这个错误,我试过很多东西,但AJAX调用永远不会成功。我试图更改URL简单, _liqui-cult.html (不含文件://基于URL)。我也试过 / _ LIQUI-cult.html

Having spent the day Googling this error, I've tried numerous things, but the AJAX call never succeeds. I've tried changing the url to simply, _liqui-cult.html (without the file:// -based url). I've also tried /_liqui-cult.html.

我开始了JQuery的 $。负载尝试,这是行不通的,所以我切换到更详细的 $。阿贾克斯电话。

I started out trying with the JQuery $.load, and that wasn't working, so I switched to the more verbose $.ajax call.

不管我做什么,我要么得到一个404的状态,或者,如果我将URL更改为 HTTP://_liqui-cult.html ,我获得0的responseText的状态,但没有。

No matter what I do, I either get a 404 as the status, or, if I change the url to http://_liqui-cult.html, I get a status of 0, but nothing in the responseText.

所以,我把JQuery的的方程,并试图简单XMLHtt prequest,像这样:

So, I took JQuery out of the equation, and tried a simple XMLHttpRequest, as so:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && (xmlhttp.status==200 || xmlhttp.status==0))
  {
    $('#data_details .description').html(xmlhttp.responseText);
    $.mobile.changePage('#screen_detail');
  }
}
xmlhttp.open("GET","_liqui-cult.html", true);
xmlhttp.send(null);

同样,我已经试过各种可能的URL模式映射到HTML文件。而且,我仍然能得到最好的是xmlhttp.responseText是空白的。

Again, I've tried every conceivable url pattern to map to the html file. And still, the best I can get is xmlhttp.responseText is blank.

因此​​,如何跨域问题?以下是我已经试过:
    <获得原产地= /&GT*;
    <获得原产=文件:// */>
    <获得原产=*/>

So how about cross-origin issues? Here is what I've tried: <access origin=".*"/> <access origin="file://*"/> <access origin="*"/>

同样,我已经试过映射的所有方式的HTML文件,这些不同的接入产地的设置,我仍然无法加载HTML文件。

Again, I've tried all ways of mapping to the html file, with those different access origin settings, and I still cannot load the html file.

任何想法?

推荐答案

更改,获取由_liqui-cult.htmlAJAX加载同名没有下划线的HTML文件的名称LIQUI-cult.html 解决了这一问题。

Changing the name of the html file that gets AJAX-loaded from "_liqui-cult.html" to the same name without the underscore "liqui-cult.html" fixed the problem.

这篇关于使用PhoneGap的,不能在Android上使用Ajax加载本地.html文件只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆