后端异步接口url

/wp-common/products.php?term_id=4&page=1


var currentAjax;
var total = 0;
function getAjaxData(term_id, page = 1) {
currentAjax = $.ajax({
type: "GET",
url: "/wp-common/products.php?term_id=" + term_id + "&page=" + page,
dataType: "json",
success: function (res) {
if (res) {
total = res.total;
if (total > 0) {
var products = res.products;
var subHtml = `
${products.map(
value => `
<li>
<a href="${value.url}">
<div class="img-wrap">
<img src="${value.image}"
alt="${value.alt}">
</div>
<h4>${value.name}</h4>
</a>
</li>`
).join('')}`;
$('.p02-8-s1 .text ul').html(subHtml);
if (total > 1) {
$('.mml-pages').mmlpage(page, total, {
prev: '<i class="fa fa-chevron-left" aria-hidden="true"></i>',
next: '<i class="fa fa-chevron-right" aria-hidden="true"></i>',
href: 'javascript:;',
"click": function (page) {
$('.p02-8-s1 .text ul').empty().html("<li>loading products...</li>");
getAjaxData(term_id,page);
}
});
$('.p02-1-s1 .prev').click(function () {
if (currentAjax) {
currentAjax.abort();
}
$('.p02-8-s1 .text ul').empty().html("<li>loading products...</li>");
page = page > 1 ? page - 1 : 1;
getAjaxData(term_id,page);
});
$('.p02-1-s1 .next').click(function () {
if (currentAjax) {
currentAjax.abort();
}
$('.p02-8-s1 .text ul').empty().html("<li>loading products...</li>");
page = page < total ? page + 1 : total;
getAjaxData(term_id,page);
});
}
if (!total ) {
$('.p02-8-s1 .text ul').empty().html("<li>no data</li>");
}
}
}
}
});
}
var term_id = $('.p02-8-s1 .tab .active').attr('data-term_id');
if ($(window).width() <= 540) {
getAjaxData(0, page = 1);
} else {
getAjaxData(term_id, page = 1);
}
$(window).resize(function () {
if ($(window).width() <= 540) {
getAjaxData(0, page = 1);
} else {
getAjaxData(term_id, page = 1);
}
});

posted on 2019-11-27 18:37  巧丽  阅读(376)  评论(0编辑  收藏  举报

导航