如何通过ajax重新加载数据

时间:2022-01-11 14:24:04

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

我有另一种方法来添加产品,我想在添加新产品时,在成功方法中它会重新加载到以前的方法listProducts()以获取新产品列表

$(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 个解决方案

#1


2  

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

#2


1  

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

#3


0  

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

使用window.location.replace(“你的产品网址”);

on the successful call of ajax function.

关于ajax功能的成功调用。

I modified your jquery below . hope this will work for you.

我在下面修改了你的jquery。希望这对你有用。

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

#1


2  

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

#2


1  

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

#3


0  

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

使用window.location.replace(“你的产品网址”);

on the successful call of ajax function.

关于ajax功能的成功调用。

I modified your jquery below . hope this will work for you.

我在下面修改了你的jquery。希望这对你有用。

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