Eclipse Phonegap无法使用jquery.load函数加载外部字符串文件

Eclipse Phonegap cannot load external string file using jquery .load function

本文关键字:加载 函数 外部 字符串 文件 load jquery Phonegap Eclipse      更新时间:2023-09-26

我有一个简单的index.html文件,使用jquery库中的.load函数显示test.txt中的字符串,并将字符串内容放入html(div class="konten")

这是以下HTML脚本:

<script type="text/javascript" charset="utf-8" src="js/lib/cordova-1.9.0.js"></script>
<script type="text/javascript" charset="utf-8" src="js/lib/jquery-2.0.2.js"></script>
...
<body onload="init();">
    <div class="center">
        <div class="konten"></div>
    </div>
</body>
...

和js:

$(document).ready(function(){
    $('.konten').load('test.txt',function(responseTxt,statusTxt,xhr){
      if(statusTxt=="success")
        alert("External content loaded successfully!");
      if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
    });
});

当我尝试在桌面浏览器上运行index.html文件(快速测试)时,没有显示test.txt的内容,并显示javascript警报错误:404:Error

我写的剧本有什么问题吗?

HTML编辑器:Eclipse 4.2.2(Juno)

cordova-1.9.0

jquery-2.0.2

您可以使用以下登录名。通过这种方式,您可以根据您使用的是桌面浏览器还是移动应用程序来调用所需的功能。

function onBodyLoad() {
    // these are useful later in the app, might as well set early
    window.isRipple = (window.tinyHippos != null);
    window.isPhoneGap = /^file:'/{3}[^'/]/i.test(window.location.href);
    window.isIOS = !window.isRipple && navigator.userAgent.match(/(ios|iphone|ipod|ipad)/gi) != null;
    window.isAndroid = !window.isRipple && navigator.userAgent.match(/(android)/gi) != null;
    // stuff I use for debugging in chrome
    if (window.isPhoneGap) {
       document.addEventListener("deviceready", onDeviceReady, false);
    } else {
       onDeviceReady();
    }
}

您可以在页面的正文加载事件中调用此函数。

<body onload="onBodyLoad()"> 

我记得在SO中看到过其他人发布的这个逻辑。真正的功劳应该归于那个用户。

在deviceready事件中而不是文档就绪事件中尝试代码。

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
   $('.konten').load('test.txt',function(responseTxt,statusTxt,xhr){
     if(statusTxt=="success")
        alert("External content loaded successfully!");
     if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
    });
}