To retrieve multiple sets of data using Ajax, you can make multiple Ajax requests, each of which retrieves a different set of data from the server. Alternatively, you can retrieve all the data in a single Ajax request by returning multiple data sets as a JSON object or array from the server, and then parsing the JSON data in JavaScript.
Here's an example of how to retrieve multiple sets of data using Ajax by making multiple Ajax requests:
$.ajax({url: "https://example.com/api/data1",type: "GET",success: function(response1) {// Handle the first set of dataconsole.log(response1);},error: function(xhr, status, error) {// Handle any errors}});
$.ajax({url: "https://example.com/api/data2",type: "GET",success: function(response2) {// Handle the second set of dataconsole.log(response2);},error: function(xhr, status, error) {// Handle any errors}});
In this example, two separate Ajax requests are made to different endpoints to retrieve two sets of data. Each Ajax request has its own `success` callback function that handles the response data.
Alternatively, here's an example of how to retrieve multiple sets of data using a single Ajax request and JSON:
$.ajax({url: "https://example.com/api/multipledata",type: "GET",success: function(response) {// Parse the JSON responsevar data1 = response.data1;var data2 = response.data2;// Handle the dataconsole.log(data1);console.log(data2);},error: function(xhr, status, error) {// Handle any errors}});
In this example, a single Ajax request is made to an endpoint that returns a JSON object containing multiple data sets. The `success` callback function handles the response data by parsing the JSON object and accessing the individual data sets. The `data1` and `data2` variables contain the individual data sets, which can then be handled separately.