如何将Json数据导入Jquery移动应用程序

我正在使用Jquery Mobile在dreamviewer中制作基本的“新闻”应用程序。

我做了所有的设计,但现在是时候将我的api服务器中的Json数据放到我的应用程序中了。

这是我的api服务器链接; fe“http:/ mywebapiurl”

我想我需要编写关于getJson的Jquery函数,但我不知道如何制作它?

我将如何将它放入我的列表视图中,带有小文章图片,articletitle和articleheadline?

这是我的列表视图的示例,我将展示来自Json的新闻。

 

Post Mobile

© funky app

这是我试过的代码示例;

   $(document).ready(function() { $.getJSON("http://mywebapiurl", function(data) { var result= ""; $.each(data.LatestNews, function(index, value) { result+= ""; }); $("#articleContainer").html(result); }) });  

等你的回答。

非常感谢你。

工作jsFiddle示例: 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('
  • ' + row.original_name+ '

    ' + row.overview+ '

  • '); }); $('#movie-data').listview('refresh'); } }

    在您的情况下,只需将dataType:“jsonp”更改为dataType:“json”

    编辑:

    document ready不应该与jQuery Mobile一起使用。 通常它会在页面加载到DOM之前触发。

    这可以通过适当的jQuery Mobile页面事件来修复,如下所示:

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

    有关页面事件及其工作原理的更多信息可以在本文中找到,为了透明,这是我的个人博客。 或者在这里找到它。

    $ .getJSON vs $ .ajax

    我更喜欢$.ajax因为它适用于jQuery Mobile ,特别是如果你需要show/hide ajax页面加载器或者像我的例子那样进行跨域调用。 但总的来说它是一样的。

    你尝试过什么? JQuery在json主题schema上很简单:

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