jquery 网页中的多个$. AJAX 调用

lxkprmvk  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(104)

如果一个网页在几天的时间内进行了多次非同步 AJAX 调用来获取更新的信息,是否有可能出现内存泄漏或许多(数千)未使用的对象挂起,从而导致浏览器或系统故障?

2sbarzqh

2sbarzqh1#

试试这个

$(document).ready(function () {
  // First AJAX call
  $.ajax({
    url: 'https://api.example.com/data1', // Replace with your API endpoint URL
    method: 'GET',
    dataType: 'json', // Set the expected data type
    success: function (data) {
      // Handle the data returned from the server
      console.log('Data 1:', data);
    },
    error: function (xhr, status, error) {
      // Handle errors if the AJAX call fails
      console.error('Error in AJAX call 1:', error);
    },
  });

  // Second AJAX call
  $.ajax({
    url: 'https://api.example.com/data2', // Replace with your API endpoint URL
    method: 'POST',
    data: {
      param1: 'value1',
      param2: 'value2',
    },
    dataType: 'json', // Set the expected data type
    success: function (data) {
      // Handle the data returned from the server
      console.log('Data 2:', data);
    },
    error: function (xhr, status, error) {
      // Handle errors if the AJAX call fails
      console.error('Error in AJAX call 2:', error);
    },
  });

  // You can add more AJAX calls as needed
});

字符串

相关问题