How to reload data using ajax

I have a way 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 way to add a product that I want to add when adding a new product to the success method, which it reloads into the previous list of Products () methods to get a new list of products

$(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** */
        }
    });
  });
});

      

+3


source to share


3 answers


Just remember 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
        }
    });
  });
});

      

+2


source


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();
      }
    });
  });
});

      

+1


source


use window.location.replace ("your url for your product");

on successful call to the ajax function.

I changed your jquery below. hope this works 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");
        }
    });
  });
});

      

0


source







All Articles