Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Post请求挂起_Node.js_Post_Request - Fatal编程技术网

Node.js Post请求挂起

Node.js Post请求挂起,node.js,post,request,Node.js,Post,Request,我有一个ASP.NETAPI后端和用户注册。我尝试使用request模块发出这个post请求,它工作正常(我可以看到数据被正确写入数据库),但浏览器只是挂起,最终显示一个空白页面 /* POST Register */ router.post('/register/account', function(req, res, next) { process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0 request.post({ headers:

我有一个ASP.NETAPI后端和用户注册。我尝试使用request模块发出这个post请求,它工作正常(我可以看到数据被正确写入数据库),但浏览器只是挂起,最终显示一个空白页面

/* POST Register */
router.post('/register/account', function(req, res, next) {
  process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
  request.post({
    headers: {'content-type' : 'application/json'},
    url: 'https://localhost:44338/account/register',
    body:JSON.stringify({
      "email" : req.body.email,
      "password" : req.body.psw
    }), 
    function(error, response, body) {
      console.log(body);
    }
  });
});

正文应该是jwt生成的令牌,我在POSTMAN上测试了post请求,它给了我一个200 OK状态,甚至在正文中显示了令牌。知道怎么了吗?理想情况下,我希望返回到索引页面,并将令牌添加到cookie和/或标头中,以便在之后执行“授权”请求。

您必须返回响应(使用
res
),或者在希望进一步处理请求时调用
next()
。如果不这样做,请求将等待(“挂起”)


在这种情况下,当内部HTTP请求返回时,应该进一步处理请求,因此修改回调。同时考虑一下错误情况。

在帖子的回调中,您需要使用
res
响应原始请求,否则它将继续挂起。像
res.status(200).send(“Request successful”)
这样简单的东西应该放在您拥有
console.log(body)

您可以在此处查看
res
的Express.js API参考

使用
res.send()
将响应发送到客户端浏览器

router.post('/register/account', function(req, res, next) {
  process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
  request.post({
    headers: {'content-type' : 'application/json'},
    url: 'https://localhost:44338/account/register',
    body:JSON.stringify({
      "email" : req.body.email,
      "password" : req.body.psw
    }), 
    function(error, response, body) {
      console.log(body);
      res.status(200).send(body);
    }
  });
});