Basically I have client side javascript which sends post requests (through jQuery) triggered by user interactions with the page to my node.js server. The node.js server then handles the requests and updates content in the database.
For some reason, I am reaching a limit of the number of posts I can send to the server in a single page load. This maximum is 6. After 6 posts are sent from a page, I get these errors for trying to send any more requests:
EDIT:
These red errors are popping up in my Javascript console after trying to send more than 6 requests:
send jquery-latest.js:8526
jQuery.extend.ajax jquery-latest.js:7978
jQuery.(anonymous function) jquery-latest.js:7614
haveLikedOrDislikedObject
(anonymous function) localhost:33
fire jquery-latest.js:1037
self.fireWith jquery-latest.js:1148
done jquery-latest.js:8074
callback
My code for sending the post: (Basically a listener is attached to numerous divs, and when it is clicked a post request is sent)
//Sets on click listener for like button of content
$(document).delegate("div[id^='likeDiv']", "click", function() {
var el = this;
$.getScript("public/javascripts/load_content.js", function(){
haveLikedOrDislikedObject(0, $(el).attr('name'), theUser);
});
});
function haveLikedOrDislikedObject(res, contentNumber, user){
if(user != undefined){
if(res == 0){
$.post("/likeContent", { content: contentNumber, user: user.UserID });
$("#haveLikedDiv_" + contentNumber).text("You like this.");
} else{
$.post("/dislikeContent", { content: contentNumber, user: user.UserID });
$("#haveLikedDiv_" + contentNumber).text("You dislike this.");
}
} else{
$("#haveLikedDiv_" + contentNumber).text("Sorry, something went wrong.");
}
};
Just wondering why I would be getting this limit? Also, any thoughts on how I can go around this, or other ways to send numerous things to my server from a single page?
SOLVED:
Turns out I was not sending back anything from my server and I think this means each post was waiting for a response, therefore making 6 open requests. So, make sure that you are sending something back from the server, even if it is undefined like this:
app.post('/likeContent', function(req, res){
res.send(undefined);
});
Copyright Notice:Content Author:「Devin Lynch」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15343295/limit-to-number-of-post-requests-javascript-node-js-jquery