How To Get Json Data into Jquery Mobile Application How To Get Json Data into Jquery Mobile Application json json

How To Get Json Data into Jquery Mobile Application


Working jsFiddle example: http://jsfiddle.net/Gajotres/8uac7/

$(document).on('pagebeforeshow', '#home', function(e, data){          $.ajax({url: "http://api.themoviedb.org/2.1/Movie.search/en/json/23afca60ebf72f8d88cdcae2c4f31866/The Goonies",        dataType: "jsonp",        async: true,        success: function (result) {            ajax.parseJSONP(result);        },        error: function (request,error) {            alert('Network error has occurred please try again!');        }    });         });var ajax = {      parseJSONP:function(result){        $.each( result, function(i, row) {            $('#movie-data').append('<li><a href="#headline"><img src="http://www.incine.fr/media/photos_films/original/1285_4bc92529017a3c57fe00ee84_1293130970.jpg" class="ui-li-has-thumb"/><h3>' + row.original_name+ '</h3><p>' + row.overview+ '</p></a></li>');        });        $('#movie-data').listview('refresh');    }}

In your case, just change dataType: "jsonp" to dataType: "json".

EDIT :

document ready should not be used with a jQuery Mobile. Usually it will trigger before a page is loaded into the DOM.

This can be fixed with an appropriate jQuery Mobile page event, like this:

$(document).on('pagebeforeshow', '#home', function(){        });

More information about page events and how they work can be found in this ARTICLE, to be transparent it is my personal blog. Or find it HERE.

$.getJSON vs $.ajax

I prefer $.ajax because it works better with jQuery Mobile, specially if you need to show/hide ajax page loader or do a cross domain call like in my example. But all together it is the same.


What have you tried? JQuery is pretty straight forward on the json topic, schema:

$(function() {    $.getJSON('/api', function(data) {        var $foo = $('#foo');        for(vari=0; i<data.length; i++) {            $foo.append('<div>'+data[i][title]+'</div>');        }    });});