Javascript如何在函数完成后触发ajax调用

时间:2017-06-15 22:23:28

标签: javascript jquery ajax phonegap

我使用Phonegap开发一个应用程序,一切都很顺利但是我想在同一页面上从另一个函数获取qr代码值后触发ajax,有没有办法在用户获取扫描后立即触发它值?代码有点乱......

    <script type="text/javascript">
            $(document).ready(function() {
 `var uemail = localStorage.getItem("semail"); 
document.getElementById("x").innerHTML = uemail;`

      var dataString = "email=" + email + "&secode=" + value + "&insert=";
            var h = localStorage.getItem("secode");
        $("#conf").click(function() {
                $.ajax({
                    type: "POST",
                    url: "http://example.com/auth.php?email="+uemail+"&secode="+h+"",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                     $('#loading').show();
                    },
                    success: function(data) {

                        if (data == "success") {

                             alert("DONNNEEE");
                        } else if (data == "error") {

                        alert("error");
                        }
                    }
                });
                return false;
                  });
                   });

      function logoutck() {
    var r = confirm("هل انت واثق من ذلك؟");
    if (r) {
       window.location.href = 'login.html'
    }
}

    function scan()
            {
                cordova.plugins.barcodeScanner.scan(
                    function (result) {
                        if(!result.cancelled)
                        {

                            if(result.format == "QR_CODE")
                            {
                             var value = result.text;
                            localStorage.setItem("secode", value);
                               var h = localStorage.getItem("secode");
                               alert(h);
                            }
                        } else{
                        alert("U CANCELLED IT");
                        }
                    },
                    function (error) {
                        alert("Scanning failed: " + error);
                    }
               );
               }


</script>

</head>

<body>
<div id="loading">

</div>
    <div class="bar bar-header bar-positive" style="margin-bottom:80px;">
        <h1 class="title">EasyQ</h1>
        <a class="button button-clear" onclick="logoutck();">خروج</a>
    </div>

    <br/>
    <br/>
    <div class="container">




    <p style="text-align: right;">:انت مسجل دخولك ك</p>

  <div align="right"><span class="badge" align="right" dir="rtl" style="text-align: right;" id="x"></span></div>
  <br>

 <a target="_blank" href="javascript:scan();" style="text-decoration: none"><button>Scan</button></a>
  <br><br><br><br>
  <button id="conf">confirmo</button>
  <script> 
  $(document).ready(function() {
  var uemail = localStorage.getItem("semail");
document.getElementById("x").innerHTML = uemail;

      var dataString = "email=" + email + "&secode=" + value + "&insert=";
            var h = localStorage.getItem("secode");
        $("#conf").click(function() {
                $.ajax({
                    type: "POST",
                    url: "http://easyq.hol.es/easyqappfiles/qrauth.php?email="+uemail+"&secode="+h+"",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                     $('#loading').show();
                    },
                    success: function(data) {

                        if (data == "success") {

                             alert("DONNNEEE");
                        } else if (data == "error") {

                        alert("error");
                        }
                    }
                });
                return false;
                  });
                   });


</script>

1 个答案:

答案 0 :(得分:0)

将您的ajax调用写为函数,并在需要时调用它。

function ajaxCall(){
  $.ajax({
    success: function(){
        // do something
    }
  });
}

然后

if(result.format == "QR_CODE") {
  ajaxCall();
}

如果需要,您还可以将参数传递给函数