简体   繁体   中英

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. One module is called 'router' and is responsible for fetching static html from local files and loading their contents into a <div> . This works on iPhone, iPad, and Windows Phone 8. But there's something strange happening in WP7.

Sample code from 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.

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. But when I put that code in my router.js , it returns the error.

Some observations:

  • router.js is in the same folder ( /www/js ) as main.js .
  • index.html is in the /www folder.
  • 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 .
  • 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. So you can attach full cordovalib instead of compiled dll and see where Cordova tries to find that file.

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);)

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

Hope that helps - it was confusing for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM