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

Lal*_*ali 3 ajax jquery jquery-mobile web cordova

我正在使用HTML5,CSS3,JavaScript和jQueryMobile制作一个移动Web应用程序,并对其进行变形Phonegap.

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

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

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

Gaj*_*res 6

使用/ $.ajax创建AJAX调用时,jQuery函数是标准函数.jQueryjQuery 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!');
        }
    });         
});
Run Code Online (Sandbox Code Playgroud)

几件事情要考虑:

  • $.ajax 在页面转换期间不应使用调用,因为可能存在页面闪烁
  • 通过AJAX调用动态生成的所有数据必须随后增强到jQuery Mobile页面标记,这是关于此主题的我的博客文章.或者可以在这里找到.
  • 当显示动态生成的内容时,必须在正确的页面事件期间附加,最好的是pageboforeshow事件.要查找有关jQuery Mobile页面事件的更多信息,请查看此文章.