getJSON function works only when ajax page loading is turned off
I struggled with this so initially I gave up and turned off ajax page loading for jQuery Mobile ($.mobile.ajaxEnables = false;) and (data-ajax="false"). I basically finished my website but I REALLY WANT TO USE AJAX LOADING!!!
My problem is when going from index.html (topics page) to news.html, and going from news.html to newstext.html, the javascript ajax calls, which should get data from my database and fill the page, aren't getting executed. There are no errors in the console. Here's my code...PLEASE HELP ME FIX THIS!!!
INDEX.HTML
- <!DOCTYPE HTML>
- <HTML>
- <head>
- <meta charset="UTF-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
- //3 files here for jquery, mobile and css
- </head>
- <body>
- <div data-role="page" id="home">
- <div data-role="content">
- <div class="ui-grid-solo">
- <div class="ui-block-a"><a class="ui-bar ui-bar-e" data-ajax="false" href="news.html" style="text-align:center" data-inline="false">Main News</a></div>
- </div>
- </div>
- </div>
- </body>
- </HTML>
NEWS.HTML
- <body>
- <div data-role="page" id="mainNews">
- <div data-role="content">
- <ul class="stuff" id="fnews" data-role="listview" data-theme="c">
- <li style="text-align:center" data-role="list-divider">Daily Topic</li>
- </ul>
- </div>
- </div>
- <script src="js/getmainnews.js"></script>
- </body>
GETMAINNEWS.JS
- var serviceURL = "http://localhost/basket/services/";
- $('#mainNews').on('pageinit', function(event) {
- $.mobile.ajaxEnabled = false;
- getNews();
- });
- function getNews() {
- $.getJSON(serviceURL + 'getmainnews.php', function(data) {
- daily = data.items;
- $.each(daily, function(index, news) {
- $('#fnews').append('<li data-icon="false"><a style="white-space:normal;" href="newstext.html?url=' + news.link + '">' + news.pic + news.article + '</a></li>');
- });
- $('#fnews').listview('refresh');
- });
- }
It works when ajax is turned off perfectly. When it's on, after clicking on Main News, in the console (Chrome) the XHR returns news.html, not the php file (getmainnews.php). When it's off, it returns the php file. I tried changing the location of the js, I tried changing on('pageinit') to bind and to live and nothing helped. Why isn't the ajax call working? Please take some time and help me out!!!