So I am using AJAX to hit an url multiple times, the problem that I am facing is that only one AJAX request gets completed even though the code is supposed to complete multiple requests. I have run the following code in the terminal:
var item_arr = ['a','b'];
item_arr.forEach(function(item_name){
$.ajax({url:'https://example.com:port/',
type:'POST',
data:{ 'item':item_name },
success:function(res){
console.log(res);
}
});
});
What I basically want to ask is:
- Is there anything that is wrong with this code?
- Or is there a limit to the number of parallel requests to a specific url (there are just two)?
Also, I am using NODEJS(request) to build the app, I just used jquery to check if it works in jquery but both of these technologies are performing similarly for this issue.
UPDATE: As Rion suggested suggested, I compared the curl request for both, successful and unsuccessful requests. The headers for successful hit look like :
'Origin: http://evil.com/'
'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.87 Chrome/49.0.2623.87 Safari/537.36'
'Content-Type: application/x-www-form-urlencoded'
'Accept: */*'
'Referer: http://stackoverflow.com/questions/33243685/nodejs-a-better-way-to-handle-ajax-request-to-the-server'
'Connection: keep-alive'
whereas for unsuccessful request has following headers:
'Accept: */*'
'Referer: http://stackoverflow.com/questions/33243685/nodejs-a-better-way-to-handle-ajax-request-to-the-server'
'Origin: http://stackoverflow.com'
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.87 Chrome/49.0.2623.87 Safari/537.36'
'Content-Type: application/x-www-form-urlencoded'
Does Accept-Encoding make any difference?