[英]Using $.ajax to fetch static html files in phonegap and Windows Phone 7
We're using Phonegap/Cordova 2.3.0 to deploy an app to Windows Phone 7. The application uses Require.js to dynamically load all modules. 我们使用Phonegap / Cordova 2.3.0将应用程序部署到Windows Phone 7.应用程序使用Require.js动态加载所有模块。 One module is called 'router' and is responsible for fetching static html from local files and loading their contents into a
<div>
. 一个模块称为“路由器”,负责从本地文件中获取静态html并将其内容加载到
<div>
。 This works on iPhone, iPad, and Windows Phone 8. But there's something strange happening in WP7. 这适用于iPhone,iPad和Windows Phone 8.但WP7中发生了一些奇怪的事情。
Sample code from router.js
: 来自
router.js
示例代码:
//path that works with WP8
var path = "www/views/";
$.ajax({ url: path + "test3.html" })
.done(function(html) {
alert(html); //result from Windows Phone 8
}).fail(function(error) {
alert(error); //result from Windows Phone 7
});
The error that's given when testing on WP7 is a 404 Not Found. 在WP7上测试时给出的错误是404 Not Found。
I say "there's something strange happening" because I can copy the exact code above to the main.js
(require.js entry point) or the index.html
and it will run without error. 我说“发生了一些奇怪的事情”,因为我可以将上面的确切代码复制到
main.js
(require.js入口点)或index.html
,它将运行而不会出错。 But when I put that code in my router.js
, it returns the error. 但是当我将该代码放入
router.js
,它会返回错误。
Some observations: 一些观察:
router.js
is in the same folder ( /www/js
) as main.js
. router.js
是在同一文件夹( /www/js
)作为main.js
。 index.html
is in the /www
folder. index.html
位于/www
文件夹中。 /app/www/views
, app/www/views
, /www/views
, www/views
, /views
, views
, and even x-wmapp0:/app/www/views
and x-wmapp1:/app/www/views
. /app/www/views
, app/www/views
, /www/views
, www/views
, /views
, views
,甚至x-wmapp0:/app/www/views
和x-wmapp1:/app/www/views
。 The following may help you to understand the problem 以下内容可帮助您了解问题
1) Access to local files on Cordova WP7 is done via File.cs (\\templates\\standalone\\cordovalib\\Commands\\File.cs).readAsText method. 1)通过File.cs(\\ templates \\ standalone \\ cordovalib \\ Commands \\ File.cs).readAsText方法访问Cordova WP7上的本地文件。 So you can attach full cordovalib instead of compiled dll and see where Cordova tries to find that file.
因此,您可以附加完整的cordovalib而不是编译的dll,并查看Cordova尝试查找该文件的位置。
2) Translation between ajax requests and local files is done at XHRPatch (cordova-2.3.0.js) so try adding uri tracing somewhere here (console.log(uri);) 2)ajax请求和本地文件之间的转换是在XHRPatch(cordova-2.3.0.js)完成的,所以尝试在这里添加uri跟踪(console.log(uri);)
With Cordova 2.7.0 that I am using now (on Windows Phone 8 - on WP7 it may differ!) it looks like you just have to eference with a path relative to the root of your application (not your www
directory! That means you must go from www/path/to/my/file.js
使用我现在使用的Cordova 2.7.0(在Windows Phone 8上 - 在WP7上它可能有所不同!)看起来你只需要通过相对于应用程序根目录的路径(而不是你的
www
目录!这意味着你必须从www/path/to/my/file.js
Hope that helps - it was confusing for me! 希望有所帮助 - 这对我来说很困惑!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.