Node.js在发送标头后无法设置标头.在另一个XHR请求中启动XHR请求时 [英] Node.js Can't set headers after they are sent. When initiating an XHR request within another XHR request

查看:114
本文介绍了Node.js在发送标头后无法设置标头.在另一个XHR请求中启动XHR请求时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js无法处理与以下jQuery/Zepto XHR模式相似的客户端代码:

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

在其他框架中之前,我已经完成了这种模式(在另一个XHR请求中发起一个XHR请求).我已经阅读过> Node.js错误:可以吗?设置标头发送后的设置,以及基于事件的Node.js服务器模型的工作方式.换句话说,第一个XHR请求没有调用res.end(),所以当第二个XHR请求被调用时,Node.js会抱怨(在连续循环中顺便说一句).

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).

我的问题是:有人能推荐一种替代模式来链接XHR请求客户端吗?我可以在Node.js服务器端做一些事情来保持现有的客户端模式吗?

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?

根据接受的答案进行更新
错误肯定是在我自己的服务器端代码中.一个简单的验证函数抛出了一个错误,但是在捕获到错误后,仅调用了res.end().由于某种原因,我曾经在调用res.end()的假设会立即停止函数的执行.在这种情况下,插入返回"会在将JSON消息发送到客户端后立即停止执行.

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. 

推荐答案

问题不是您想的那样.由于回调,您的两个XHR以串行方式而不是并行方式发生.在第一个请求的整个请求/响应过程完成之前,第一个的success回调将不会触发(node.js已经调用response.end()并且浏览器已接收并解析了响应).只有这样,第二个XHR才开始.您拥有的客户端AJAX模式很好.它将与node.js和任何其他Web服务器同样良好地工作.

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.

您的问题出在服务器端的node.js代码中,但这不是节点中的错误或限制,而是您自己的编程错误.发布您的服务器端代码,我们可以帮助您进行跟踪.对于node.js初学者来说,通过简单的编码错误就可以解决此错误.

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.

这篇关于Node.js在发送标头后无法设置标头.在另一个XHR请求中启动XHR请求时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆