简体   繁体   English

发布请求完成后如何重定向用户

[英]How to redirect the user after the post request finished

I'm using Node.js and express and I want to send a POST Request to a webserver, wait for the result and then redirect the user to a different page. 我正在使用Node.js进行表达,我想将POST请求发送到Web服务器,等待结果,然后将用户重定向到另一个页面。

My problem is that the res.redirect('xxx'); 我的问题是res.redirect('xxx'); gets executed before the post request is complete. 在发布请求完成之前被执行。 And in the function res1.on('end', function () { I can't do a res.redirect() . function res1.on('end', function () {我无法执行res.redirect()

So how do i do a redirect in the on end function? 那么我该如何在on函数中进行重定向?

app.post('/', function(req,res,next){

  console.log(req.body.user);
  console.log(req.body.pw);
  console.log("-------- POST -------------");

  // form data
  var postData = querystring.stringify({
    grant_type: "password",
    client_id: "xxx",
    client_secret:"xxx",
    username:req.body.user,
    password:req.body.pw
  });

  var options = {
    host: 'xxx',
    port: 443,
    method: 'POST',
    path: '/oauth_token.do',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': postData.length
    }
  };

  var req = https.request(options, function (res1) {
    result = '';
    res1.on('data', function (chunk) {
      result += chunk;
    });
    res1.on('end', function () {
     var obj = JSON.parse(result);
     console.log(obj.access_token);
     access_token = obj.access_token;
     });
    res1.on('error', function (err) {
      console.log("Error 1");
    })
  });

  req.write(postData);
  req.end();

 // res.redirect('xxx');

});

You wanna do it in the callback so that you know it's done: 您想在回调中执行此操作,以便知道已完成:

res1.on('end', function() {
    var obj = JSON.parse(result);
    console.log(obj.access_token);
    access_token = obj.access_token;
    // res.redirect('xxx');
});
res1.on('error', function(err) {
    console.log("Error 1");
})

whenever response was returned from the server, it goes to callback function so write your redirect logic in the callback so that you know it's done. 无论何时从服务器返回响应,它都会进入回调函数,因此在回调中编写重定向逻辑,以使您知道已完成。

res1.on('end', function () {
     var obj = JSON.parse(result);
     console.log(obj.access_token);
     access_token = obj.access_token;

     //call your redirect function after success response 
    // res.redirect('xxx');

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用 fetch() POST 请求登录后重定向用户? - How to redirect the user after login using fetch() POST request? 如何在 Nodemailer POST 请求后重定向 - How to redirect after Nodemailer POST request 如何从express \\ nodeJS中的发布请求中获取数据,并在提交后将用户重定向到html文件 - how to get data from post request in express\nodeJS and redirect user to html file after submit 收到来自 fetch POST 请求的响应后,如何将用户重定向到页面? - How to redirect user to a page after receiving the response from a fetch POST request? $ .post()之后执行的代码,后请求完成之前 - Code after $.post() is executed before post-request is finished 节点 - 如何获取 POST 请求参数,然后快速重定向用户 - node - How to get POST request params and then redirect user in express 如何在提取后调用后管理重定向请求 - How to manage a redirect request after a fetch post call 在Angular / .NET MVC中发出发布请求后,如何使用模型重定向? - How to redirect with a model after making a post request in Angular/.NET MVC? 使用Needle发送post请求后,将用户重定向到REST API的URL - Redirect the user to the URL of REST API after sending the post request using Needle 在Apps脚本中处理POST请求后重定向 - Redirect after processing a POST request in Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM