我正在使用 phonegap + html5 + JS + CSS 开发移动应用程序。对于这个应用程序,我需要从远程主机加载一个大的 JSON(大小为 200-500kb)文件,如“www.mysite.com/file.json”。
这是我尝试从 localhost 运行的 index.html 文件中的代码。
<script type="text/javascript">
var myAPI = "http://www.mysite.com/file.json?callback=?"; //for this one .done() does not execute
// var myAPI = "file.json" //but for this one .done() executes
var json = $.getJSON( myAPI, {
format: "json"
}).done(function( data ) {
/*for remote link nothing inside this done() executes but works for local link*/
$.each( data, function( i, item ) {
$("#someDiv").append("<div>"+item.id+" "+item.name+"</div>")
})
});
</script>
我使用 jquery getJson() 然后 done() 在加载完成后显示加载的 json 数据。但是当我将 file.json 文件放在本地目录中时,唯一一次 done() 执行。使用远程链接时 done() 不执行。我使用 chrome dev-tool 断点检查了它。没有 Access-Control-Allow-Origin 错误,并且 file.json 已正确加载,我可以从 devtool 的网络(file.json 加载时间)和 Source(file.json 显示在 www.mysite.com 文件夹中)选项卡中确保这一点。
任何建议我应该如何使用 jquery 从远程主机加载 json 文件。提前致谢。