Node.js cannot process my client code that runs something similar to the jQuery / Zepto XHR template below:
$.ajax({ type: 'POST', url: '/someUrl', success: function(response) { $.ajax({
I did this (initiating an XHR request in another XHR request) earlier in a different framework. I read about Node.js Error: it is not possible to set headers after they are sent and how the event-based Node.js server model works. In other words, the first XHR request did not call res.end (), therefore, when the second XHR request is called Node. js complains (in btw continuous loop).
My questions is: can anyone recommend an alternative template for binding client-side XHR requests? Is there something I can do Node.js on the server side to keep the existing template on the client side?
Update based on accepted answer
The error is definitely in my own server side code. A simple check function was an error, but only res.end () was called after it was detected. For some reason, the assumption I had called res.end () immediately stopped the execution of the function. In this case, the insert "return" stops execution immediately after sending the JSON message to the client.
if (_.isEmpty(req.body)) { res.end(JSON.stringify({'Error':'POST required'})); // suppose 'return' is needed here as well return } else { try { if (_.has(req.body, 'id')) { id = parseInt(req.body['id']); } else { throw {'Error':'Missing param in req.body'}; } // end if } catch(err) { res.end(JSON.stringify({'Error':'Missing key(s)','Keys':_.keys(req.body)})); // without a return here, the code below this 'do some more work' would // be executed return } // end else // do some more work // without the above 'return' the code would // a) make a database call // b) call res.end() again!!! <-- bad.
exshovelrydr Feb 17 '12 at 17:22 2012-02-17 17:22
source share