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.

Answers


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!


Need Your Help

Many linear regressions

r for-loop time-series regression apply

As part of my data analysis (on time series), I am checking for correlation between log-returns and realized volatility.

C# regular expressions - matching whole words?

c# regex

I am writing a program that needs to search a LARGE text document for a large collection of words. The words are all file names, with underscores in them (eg, this_file_name). I know how to open ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.