简体   繁体   English

使用$ .ajax在phonegap和Windows Phone 7中获取静态html文件

[英]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文件夹中。
  • I've tried about every path combination I could think of hoping to stumble across one that worked. 我已经尝试过我能想到的每一个路径组合,希望偶然发现一个有效的路径。 /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/viewsapp/www/views/www/viewswww/views/viewsviews ,甚至x-wmapp0:/app/www/viewsx-wmapp1:/app/www/views
  • Obviously, I have provided a super simplified example. 显然,我提供了一个超简化的例子。 If you need more, just tell me. 如果您需要更多,请告诉我。

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.

相关问题 使用PhoneGap时,Windows 8 Phone是否支持JQuery ajax调用? - Does Windows 8 Phone support JQuery ajax call when using PhoneGap? 第二个ajax调用在Phonegap Jquery移动Windows Phone 8中不起作用 - Second ajax call not working in Phonegap Jquery mobile Windows phone 8 无法在PhoneGap Windows Phone 8中将图像加载到HTML5画布 - Can't load images to a HTML5 canvas in PhoneGap Windows Phone 8 Windows Phone 8和phonegap 2.7.0,在HTML页面和Xaml之间以及后退之间导航 - Windows phone 8 and phonegap 2.7.0, Navigation between HTML pages and Xaml and back 在Windows Phone 7的PhoneGap / Cordovia上以HTML显示LocalStore中的图像 - Showing an image from LocalStore in HTML on PhoneGap/Cordovia on Windows Phone 7 如何使用插件在Windows Phone的phonegap中显示加载对话框? - How to show loading dialog in phonegap in windows phone using plugins? 使用JavaScript和PhoneGap的Windows Phone 7模拟器上不会触发警报 - Alert doesn't fire on Windows Phone 7 emulator using JavaScript and PhoneGap Windows Phone 8上的PhoneGap和外部链接 - PhoneGap and External Links on Windows Phone 8 通过使用phonegap android中的“后退”按钮从images.html返回index.html - return to index.html from images.html by using phone back button in phonegap android Phonegap Windows Phone 8自定义Web字体 - Phonegap Windows phone 8 custom web-font
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM