Can i call Mulitple ajax in one method when i am post data then get automatically.
Loading
Can i call Mulitple ajax in one method when i am post data then get automatically.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 1, 2023, 8:55 AM EDT
Yes, you can make multiple AJAX (Asynchronous JavaScript and XML) requests within a single method, and you can set them up to be executed sequentially or concurrently, depending on your requirements.
Here's a basic example of how you can make multiple AJAX requests using JavaScript and jQuery:
function makeMultipleAjaxRequests() { // First AJAX request (POST data) $.ajax({ url: 'url1', type: 'POST', data: { key1: 'value1' }, success: function(data1) { // Handle the response from the first request console.log(data1); // Second AJAX request (GET data) $.ajax({ url: 'url2', type: 'GET', data: { key2: 'value2' }, success: function(data2) { // Handle the response from the second request console.log(data2); // Continue with more requests if needed }, error: function(error2) { console.error(error2); } }); }, error: function(error1) { console.error(error1); } }); } // Call the function to initiate the requests makeMultipleAjaxRequests();In this example, the first AJAX request is a POST request, and the second one is a GET request. You can nest these requests if you want them to be executed sequentially. However, if you need to make the requests concurrently, you can use
$.ajaxor$.getor$.postmethods to create multiple requests and then usePromise.allor other mechanisms to handle their responses asynchronously.Here's an example using
Promise.allto execute multiple requests concurrently:function makeMultipleAjaxRequests() { const request1 = $.ajax({ url: 'url1', type: 'POST', data: { key1: 'value1' } }); const request2 = $.ajax({ url: 'url2', type: 'GET', data: { key2: 'value2' }); // Execute requests concurrently Promise.all([request1, request2]) .then(function (responses) { const data1 = responses[0]; const data2 = responses[1]; // Handle responses here console.log(data1); console.log(data2); }) .catch(function (errors) { // Handle errors if any request fails console.error(errors); }); } // Call the function to initiate the requests makeMultipleAjaxRequests();This will send both requests simultaneously and handle their responses when they are ready. You can extend this pattern to make more requests as needed.