. how to get multiple responses while passing single request to ajax. I'm struggling to use ajax concept. Can anyone share sample code?
Asked
Active
Viewed 2,358 times
1 Answers
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
-
oh i see . know only i know about this clearly. thanks for ur concern dear wasif Khan – Clament Christopher E Jul 11 '17 at 11:31
-
@ClamentChristopherE Anytime, please accept this answer as well :) – Wasif Khan Jul 11 '17 at 11:33
-
can u send any sample code how to send multiple request and get multiple responses – Clament Christopher E Jul 11 '17 at 11:40
-
okay, I will write sample code of jquery. – Wasif Khan Jul 11 '17 at 11:44
-
Thanks for ur good response dear Wasif Khan – Clament Christopher E Jul 12 '17 at 04:55
-
When i use this JSON code it shows some error 'Uncaught ReferenceError: $ is not defined'. I think i have to import jquery file. But which of the import file i want? @wasif Khan – Clament Christopher E Jul 12 '17 at 05:39
-
use this one. – Wasif Khan Jul 12 '17 at 05:54