Using $ .ajax to extract static html files in phonegap and Windows Phone 7

We use Phonegap / Cordova 2.3.0 to deploy the application on Windows Phone 7. The application uses Require.js to dynamically load all modules. One module is called a “router” and is responsible for collecting static html from local files and loading their contents into a <div> . It works on iPhone, iPad and Windows Phone 8. But there is 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 occurs when testing on WP7 is not 404 Not Found.

I say “something strange” because I can copy the exact code above into main.js (the require.js entry point) or index.html and it will work without errors. But when I put this code in my router.js , it returns an error.

Some observations:

  • router.js is located in the same folder ( /www/js ) as main.js
  • index.html is located in the /www folder.
  • I tried every combination of paths, I could think of stumbling onto the 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 presented a simplified example. If you need more, just tell me.
+4
source share
2 answers

The following may help you understand the problem.

1) Access to local files on Cordova WP7 is via File.cs (\ templates \ standalone \ cordovalib \ Commands \ File.cs) .readAsText. That way, you can attach the full corvava instead of the compiled DLL and see where Cordoba is trying to find this file.

2) Translation between ajax requests and local files is done in XHRPatch (cordova-2.3.0.js), so try adding a uri trace somewhere here (console.log (uri);)

+3
source

With Cordova 2.7.0, which I use now (on Windows Phone 8 - on WP7 this may differ!) It seems that you just need to choose the path relative to the root of your application (and not your www directory. This means that you must go from www/path/to/my/file.js

Hope this helps - it baffled me!

0
source

All Articles