【问题标题】:Send ajax request when app close应用关闭时发送 ajax 请求
【发布时间】:2017-09-24 22:03:17
【问题描述】:

我正在使用 jquery mobile 开发cordova,我需要当用户暂停或关闭应用程序时,它会向服务器发送一个 ajax 请求。我加了

document.addEventListener("pause",  function(){


        if(!isPayed){
            request = $.ajax({
                url: URL + "removeSit.php",
                type: 'POST',
                data: datastring,
                crossDomain: false,
                cache: false,
                success: function(result){

                    $.mobile.navigate("#homePage");

                },
                error: function(error){

                }
            }); 

        }
    }, false);

如果我暂停应用程序,它可以工作,但如果我立即关闭应用程序(通过菜单按钮),它不会发送请求。我也尝试了事件“菜单按钮”,但结果相同。

【问题讨论】:

    标签: jquery ajax cordova jquery-mobile


    【解决方案1】:

    1.App关闭ajax调用

    你必须阻止关闭应用程序

    function onDeviceReady(){
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    function onBackKeyDown(){
        return false;
        //////////// your ajax call here
        $.ajax({
            url: "demo_test.txt", 
            success: function(result){            
            navigator.app.exitApp();  ///// on success close app
        }});
    }
    

    2.App暂停ajax调用

    document.addEventListener("pause", onPause, false);    
    function onPause() {
        /// your ajax call here
    }
    

    如果用户从任务管理器中杀死最小化的应用程序,则应用程序无法发送 ajax 请求。 为此,您必须设置间隔来检查应用程序是否关闭

    【讨论】:

      猜你喜欢
      • 2020-09-23
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 2020-04-05
      • 2016-07-02
      • 2012-05-24
      • 2013-06-08
      • 2023-03-23
      相关资源
      最近更新 更多