How do I get Ajax working with Phonegap?
Hi,
I am a designer/front-end developer building a PhoneGap app as my thesis project. I want to use JQM swipe page navigation
http://view.jquerymobile.com/master/demos/swipe-page/ but can't get it to work as I get the following error (only showing one example but I get the same for every Ajax-based link on the page):
I am using jquery-1.9.1.min.js and
jquery.mobile-1.4.2.min.js and have also added a few options that I've found in another post here on the forum. The scripts in my <head> are
-
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( document ).bind( "mobileinit", function() {
$.mobile.allowCrossDomainPages = true;
$.support.cors = true;
$.mobile.pushStateEnabled = false;
});
</script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
Non-Ajax links work fine.
The General PhoneGap config.xml has a tag
- <access origin="http://127.0.0.1*" />
Here is a code example from one of my pages:
- <body>
- <div id="navigation" class="cards" data-role="page" data-dom-cache="true" data-next="tools" data-url="navigation">
- <div role="main" class="ui-content">
- <div class="instruction">
- <h1>In-app navigation</h1>
- <p>To navigate through this tutorial <strong>swipe</strong> left or right.</p>
- </div>
- <div class="instruction">
- <p>To access <strong>compass</strong> view tap the compass <img src="img/compass-black.png" alt="compass"> icon at the bottom of the screen.</p>
- </div>
- <div class="instruction">
- <p>To get back to the <strong>instructions</strong> tap the questionmark <img src="img/questionmark-black.png" alt="question mark"> icon.</p>
- </div>
- </div>
- </div><!-- /page -->
- <div id="footer" data-role="footer" data-position="fixed" data-theme="a" role="contentinfo" class="ui-footer ui-bar-a ui-footer-fixed slideup" style="">
- <div data-role="navbar" class="ui-navbar" role="navigation">
- <ul class="footer-nav ui-grid-c">
- <li class="home ui-block-a"><a href="index.html" data-ajax="false" data-prefetch="true" data-transition="fade" class="ui-btn ui-btn-active"></a></li>
- <li class="back ui-block-b"><a data-rel="back" href="#" data-prefetch="true" data-transition="flip" class="ui-btn"></a></li>
- <li class="compass-view ui-block-c"><a data-ajax="false" href="compass.html" data-prefetch="true" data-transition="turn" class="ui-btn"></a></li>
- </ul>
- </div><!-- /navbar -->
- </div><!-- /footer -->
- </body>
I have amended the JS file by replacing the .demo-page class with .cards and because I don't use a header toolbar or a pop-up I've commented out
- $( "#header" ).toolbar({ theme: "b" });
and
- // $( "#trivia-button" ).attr( "href", "#" + thePage.find( ".trivia" ).attr( "id" ) );
- // We use the same header on each page
- // so we have to update the title
- // $( "#header h1" ).text( title );
I realise that this is not a PhoneGap forum but I would be grateful for pointers on how to make this work. I have seen references to JSONP and the likes but perhaps the Swipe Navigation JS can be modified to use non-Ajax linking as well? Please note that I am new to both JavaScript and PhoneGap so be gentle :)
Thanks in advance!