1

. how to get multiple responses while passing single request to ajax. I'm struggling to use ajax concept. Can anyone share sample code?

1 Answers1

2

AJAX is Asynchronous JavaScript And XML. when you send a request to server you will get 1 response for it, you need to be clearfy your concept about asynchronous http calls.

Asynchoronous calls means, for example you send two requests two server named as Request1 and Request2 but you may receive response for Request2 first then for Request1.

Sample Code for multiple requests:

<script>
function firstMethod(){
$.ajax({
  url: "http://localhost3000/firstmethod",
  data: {"data1":"abc"},
  success: function(){
    alert("Response for first request")
},
  dataType: "Application/JSON"
});
}

function secondMethod(){
$.ajax({
  url: "http://localhost3000/secondmethod",
  data: {"data2":"def"},
  success: function(){
    alert("Response for second request")
},
  dataType: "Application/JSON"
});
}
</script>

HTML

<button onclick="firstMethod()">First Request</button>
<button onclick="secondMethod()">Second Request</button>
Wasif Khan
  • 956
  • 7
  • 25