Reputation: 2023
I have a method to get all the products
function listProducts() {
$.get("./listProducts", function(data) {
data.forEach(function(item) {
var $clone = $('#product').clone().removeAttr('id');
$clone.find('.productName').text(item.nameEn);
$clone.appendTo('#rowProducts');
});
});
};
I have another method to add a product, i want when adding a new product, in the success method it does reload to the previous method listProducts() to get the new product list
$(document).ready(function() {
$('#validProduct').on("click", function(e) {
......
$.ajax({
type : "POST",
contentType : "application/json",
url : "./addProduct",
data : JSON.stringify(product),
dataType : 'json',
timeout : 6000,
success : function(data) {
/* **reload listProducts** */
}
});
});
});
Upvotes: 3
Views: 3821
Reputation: 301
Try this code...
$(document).ready(function() {
$('#validProduct').on("click", function(e) {
......
$.ajax({
type: "POST",
contentType: "application/json",
url: "./addProduct",
data: JSON.stringify(product),
dataType: 'json',
timeout: 6000,
success: function(data) {
//recall this function
listProducts();
}
});
});
});
Upvotes: 1
Reputation: 151
use window.location.replace("your lising product url");
on the successful call of ajax function.
I modified your jquery below . hope this will work for you.
$(document).ready(function() {
$('#validProduct').on("click", function(e) {
......
$.ajax({
type : "POST",
contentType : "application/json",
url : "./addProduct",
data : JSON.stringify(product),
dataType : 'json',
timeout : 6000,
success : function(data) {
/* **reload listProducts** */
window.location.replace("your lising product url");
}
});
});
});
Upvotes: 1
Reputation: 1978
Just recall the function
$(document).ready(function() {
$('#validProduct').on("click", function(e) {
......
$.ajax({
type : "POST",
contentType : "application/json",
url : "./addProduct",
data : JSON.stringify(product),
dataType : 'json',
timeout : 6000,
success : function(data) {
listProducts();//just recall the function here
}
});
});
});
Upvotes: 2