如何控制此ajax函数的完成? [英] How do I control the completion of this ajax function?
问题描述
我已经学习了一个星期的ajax,并且试图弄清楚我希望如何完成ajax功能.
I have been learning ajax for a week and I am trying to figure out how I hope to finish the ajax function.
在jquery的函数中,我获得了索引中包含的几个按钮的ID,该ID由Ajax发送到名为"task-category.php'"的外部文件,它唯一要做的就是一个sql.查询到我的数据库,它以JSON的形式返回其答案,当我收到JSON时,我会对其进行遍历,并同时在索引中使用ajax进行打印.
In my function of jquery I get the id of several buttons that I have in my index, that id sent it by ajax to an external file called "task-category.php '" that the only thing it does is an sql query to my database and it returns its answer in the form of JSON, when I receive the JSON I go through it and I'm printing at the same time with ajax in my index.
当我想知道何时在索引屏幕上完成将所有json ajax打印以显示搜索已成功进行"或未找到数据"时,就会出现我的问题.
My problem arises when I want to know in what moment I have finished traveling all the json ajax to print on the index screen that "the search has been carried out successfully" or "no data was found".
$(document).ready(function() {
$('ul#categoria li').click(function() {
let categoria = $(this).attr('id');
$.ajax({
url: 'task-categoria.php',
data: {categoria},
type: 'POST',
success: function (response) {
if(!response.error) {
let tasks = JSON.parse(response);
let template = '';
tasks.forEach(task => {
template += `<a href='index.php?idrec=${task.idrec}'>
<div class="img-container grid-item">
<img class="img-thumbnail" src="${task.imag}">
<div class="overlay">
<span>${task.tit}</span>
</div>
</div>
</a>`
});
$('#grid-container').html(template);
}
}
})
});
});
推荐答案
ajax的流向是这样
the flow of ajax is like this
您可以使用成功进行主要输出,然后使用 complete 表示已完成
you can use success to do main output and then complete to say its completed
这篇关于如何控制此ajax函数的完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!