6

I'm making basic "news" application in dreamviewer with Jquery Mobile.

I made all design but now its time to get the Json data from my api server into my application.

Here is my api server link ; f.e. "http:/mywebapiurl"

i think i need to write Jquery function about getJson but i dont know how to make it ?

How will i put it in to my listview with small article picture, articletitle and articleheadline ?

Here is sample of my listview which i'm going to show the news coming from Json.

<body> 
<div data-role="page" id="home">
<div data-role="header">
    <h1>Post Mobile</h1>
</div>
<div data-role="content">   
    <ul data-role="listview" data-inset="true">
                     <li><a href="#headline">
         <img src=ArticlePicture" class="ui-li-has-thumb"/>
         <h3>ArticleTitle</h3>
         <p>ArticleHeadline</p>
                     </a></li>
                </ul>       
</div>
      <div data-role="footer"  data-position="fixed">
      <h4>&copy; funky app </h4>
</div>

here is the code sample i tried ;

    <script>
  $(document).ready(function() {
   $.getJSON("http://mywebapiurl", function(data) {
      var result= "";
      $.each(data.LatestNews, function(index, value) {
result+= "<ul><li><a href='#'><h3>"+value.TITLE+"</h3><p>"+value.SPOT+"</p></a></li></ul>"; 
});
    $("#articleContainer").html(result);
})
});
</script>

waiting your answer.

Thank you very much.

Gajotres
  • 57,309
  • 16
  • 102
  • 130
Kuthay Gumus
  • 764
  • 3
  • 12
  • 30

2 Answers2

15

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.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
0

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>');
        }
    });
});
Imperative
  • 3,138
  • 2
  • 25
  • 40
  • You do not need to help me. And i'm not asking to anyone for whole work. Read carefully, i just need to learn how can i direct my json data to listview in jquery mobile. – Kuthay Gumus Mar 18 '13 at 12:35