Javascript 如何在函数完成后触发 ajax 调用
Javascript how to trigger ajax call after a function finishes
我正在使用 Phonegap 开发应用程序,一切正常,但我希望在从同一页面的另一个函数获取二维码值后触发 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>
将您的 ajax 调用写成一个函数,并在需要时调用它。
function ajaxCall(){
$.ajax({
success: function(){
// do something
}
});
}
然后
if(result.format == "QR_CODE") {
ajaxCall();
}
如果需要,您也可以将参数传递给函数
我正在使用 Phonegap 开发应用程序,一切正常,但我希望在从同一页面的另一个函数获取二维码值后触发 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>
将您的 ajax 调用写成一个函数,并在需要时调用它。
function ajaxCall(){
$.ajax({
success: function(){
// do something
}
});
}
然后
if(result.format == "QR_CODE") {
ajaxCall();
}
如果需要,您也可以将参数传递给函数