mas*_*m64 13 javascript php ajax jquery
我正在尝试编写一个JS代码,如果数据库中已存在给定的数字,它将取消"btn_submit"按钮.onclick事件.我使用AJAX查询给定数字的数据库,并确定是否应该将数据发送到将上传问题的.php站点.为了确定这一点,我需要numOfRows变量的值,但因为我在AJAX中设置它将保持为0. validation()函数将在我的AJAX查询完成之前完成,这会导致问题始终表明给定的数字不是存在于DB中(numOfRows将始终保持为0).在我将validation()函数的结束行中的numOfRows与0进行比较之前,如何等待AJAX查询完成?如果数字中已存在该数字,我需要将false返回到此行:
document.getElementById("btn_submit").onclick = validation;
谢谢!
var textAreaList;
var numOfRows = 0;
var finished = false;
document.getElementById("btn_submit").onclick = validation;
textAreaList = document.getElementsByClassName("text_input");
function validation() {
loadNumRows();
try {
document.getElementById('failure').hidden = true;
}
catch(e) {
console.log(e.message);
}
textAreaList = document.getElementsByClassName("text_input");
var failValidation = false;
for (var i = 0; i < textAreaList.length; i++) {
console.log(textAreaList[i]);
if (textAreaList[i].value == "") {
textAreaList[i].style.border = "2px solid #ff0000";
failValidation = true;
} else {
textAreaList[i].style.border = "2px solid #286C2B";
}
}
return !(failValidation || numOfRows != 0);
}
function loadNumRows(){
$.ajax({
url: 'php/SeeIfNumberExists?number=' + document.getElementById('number_inp').value,
type: "GET",
cache: false,
success: function (html) {
numOfRows = parseInt(html);
}
});
}
Run Code Online (Sandbox Code Playgroud)
pra*_*nde 14
永远不要使用async:false它的危险,你的应用程序可能会行为不当。
仅当您的响应返回承诺时,您才能使用await。
不幸的是,jQuery ajax 在完成时不会返回 Promise。
但是您可以在ajax请求中使用promise并在完成时返回promise。
function asyncAjax(url){
return new Promise(function(resolve, reject) {
$.ajax({
url: url,
type: "GET",
dataType: "json",
beforeSend: function() {
},
success: function(data) {
resolve(data) // Resolve promise and when success
},
error: function(err) {
reject(err) // Reject the promise and go to catch()
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
我们已经将 ajax 调用转换为 Promise,所以现在我们可以使用 wait。
try{
const result = await asyncAjax('your url');
} catch(e){
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
Ter*_*rry 13
使用async: false是一个非常糟糕的主意,并且首先打败了使用AJAX的全部目的--AJAX意味着异步.如果要在进行AJAX调用时等待脚本的响应,只需使用延迟对象和承诺:
var validation = function () {
var numberCheck = $.ajax({
url: 'php/SeeIfNumberExists?number=' + $('#number_inp').val(),
type: "GET"
});
// Listen to AJAX completion
numberCheck.done(function(html) {
var numOfRows = parseInt(html),
textAreaList = $('.text_input'),
finished = false;
// Rest of your code starts here
try {
document.getElementById('failure').hidden = true;
}
catch(e) {
console.log(e.message);
}
// ... and the rest
});
}
// Bind events using jQuery
$('#btn_submit').click(validation);
Run Code Online (Sandbox Code Playgroud)
我在你的代码中看到你正在使用本机JS和jQuery的混合 - 如果你坚持一个:)它会有所帮助
小智 6
这对我有用
async function doAjax() {
const result = await $.ajax({
url: "https://api.exchangerate-api.com/v4/latest/USD",
type: 'GET',
});
return result;
}
async function tt(){
var res = await doAjax()
var money = res.rates.INR
console.log(money)
}
tt()
Run Code Online (Sandbox Code Playgroud)
async/await与Babel这样的编译器结合使用,以使其在旧版浏览器中运行。您还必须从npm安装此Babel预设和polyfill:npm i -D babel-preset-env babel-polyfill。
function getData(ajaxurl) {
return $.ajax({
url: ajaxurl,
type: 'GET',
});
};
async function test() {
try {
const res = await getData('https://api.icndb.com/jokes/random')
console.log(res)
} catch(err) {
console.log(err);
}
}
test();
Run Code Online (Sandbox Code Playgroud)
或.then回调只是编写相同逻辑的另一种方式。
getData(ajaxurl).then(function(res) {
console.log(res)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27104 次 |
| 最近记录: |