我应该在jQueryMobile中使用jQuery Ajax api吗?

时间:2013-02-22 09:36:01

标签: jquery ajax cordova jquery-mobile web

我正在使用HTML5,CSS3,JavaScript和jQueryMobile制作移动网络应用程序,并使用Phonegap进行变形。

我是所有网络新手,我想知道,当我使用jQueryMobile进行UI时,我可以使用jQuery api进行Ajax调用,还是jQueryMobile拥有它自己的工具。

我需要使用Ajax,与外部Web服务进行交互,我将从数据库中获取(获取)和更新(获取/发布)。

换句话说,jQueryMobile是否支持所有jQuery api,还是我还要在我的应用程序中单独包含jQuery。

1 个答案:

答案 0 :(得分:6)

使用 $.ajax / <创建 AJAX 来电时,jQuery函数 jQuery 是标准功能强> jQuery Mobile

使用 jsFiddle 示例:http://jsfiddle.net/Gajotres/jLdFj/

$('#index').live('pagebeforeshow',function(e,data){ 
    $.ajax({url: "http://api.themoviedb.org/2.1/Movie.search/en/json/23afca60ebf72f8d88cdcae2c4f31866/The Goonies",
        dataType: "jsonp",
        jsonpCallback: 'successCallback',
        async: true,
        beforeSend: function() {
            $.mobile.showPageLoadingMsg(true);
        },
        complete: function() {
            $.mobile.hidePageLoadingMsg();
        },
        success: function (result) {
            ajax.parseJSONP(result);
        },
        error: function (request,error) {
            alert('Network error has occurred please try again!');
        }
    });         
});

很少有事情需要考虑:

  • $.ajax 调用不应在页面转换期间使用,因为可能会出现页面闪烁
  • 通过 AJAX 调用动态生成的所有数据必须后来增强为 jQuery Mobile 页面标记,这是我的博客 {{ 3}} 关于这个主题。或者可以找到 ARTICLE
  • 当显示动态生成的内容时,必须在正确的页面事件期间附加,最好的是 pageboforeshow 事件。要详细了解 jQuery Mobile 页面活动,请查看 HERE