Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:42:19+00:00 2026-05-29T23:42:19+00:00

Node.js can’t handle my client code that performs something similar to jQuery/Zepto XHR pattern

  • 0

Node.js can’t handle my client code that performs something similar to jQuery/Zepto XHR pattern below:

$.ajax({
  type: 'POST',
  url: '/someUrl',
  success: function(response) {
     $.ajax({  // ... do another XHR

I’ve done this (initiating an XHR request within another XHR request) pattern before within other frameworks. I’ve read about Node.js Error: Can’t set headers after they are sent and how the event-based model of Node.js server works. In other words, the first XHR request hasn’t called res.end() so when the second XHR request is called Node.js complains (in a continuous loop btw).

My questions are: Would anyone be able to recommend an alternative pattern to chaining XHR requests client-side? Is there something I can do Node.js server-side to keep the existing client-side pattern?

Update Based On Accepted Answer
The mistake is certainly in my own server side code. A simple validation function was throwing an error but upon catching it, only res.end() was called. For some reason the assumption I had was calling res.end() would immediately stop the execution of the function. In this case, inserting a ‘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''s the code would
// a) make a database call
// b) call res.end() again!!! <-- bad. 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:42:20+00:00Added an answer on May 29, 2026 at 11:42 pm

    The problem is not what you think it is. Your two XHRs happen in serial, not parallel, due to the callbacks. The success callback of the first one won’t fire until the entire request/response process is complete for the first request (node.js has already called response.end() and the browser has received and parsed the response). Only then does the second XHR commence. The client side AJAX pattern you have is fine. It will work with node.js and any other web server equally well.

    Your problem is in your server side node.js code, but it’s not a bug or limitation in node, it’s a programming mistake on your part. Post your server side code and we can help you track it down. It is very common for node.js beginners to hit this error with a simple coding mistaking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which returns InnerXML for a XMLNode. The node can contain
The B-tree is of order 4, meaning that a node can hold 4 pointers,
I am working on a node-graph-view similar to Maya's HyperGraph in which I can
I have a binary tree where each node can have a value. I want
I have a N-Ary non sorted in any way tree and each node can
I want to have a tree in memory, where each node can have multiple
I have a treeView , and i want to know if some node can
My application has two node types: a parent node which can hold recursive child
Which algorithm do you recommend to find out the nearest node which can be
How can I find out which node in a tree list the context menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.