如何使通过NodeJS到端点Ajax请求NodeJS、Ajax

由网友(誰能久伴不赱)分享简介:我使用NodeJS。我的一个功能(允许称之为funcOne)收到一些投入,我传递给另一个功能(允许调用它funcTwo)产生一些输出。I am using NodeJS. One of my function (lets call it funcOne) receives some input which I pas...

我使用NodeJS。我的一个功能(允许称之为funcOne)收到一些投入,我传递给另一个功能(允许调用它funcTwo)产生一些输出。

I am using NodeJS. One of my function (lets call it funcOne) receives some input which I pass to another function (lets call it funcTwo) which produces some output.

在我通过输入funcTwo我需要做一个Ajax调用端点传递输入,然后我必须通过AJAX调用来funcTwo产生的输出。 funcTwo应该被称为只有当AJAX调用成功。

Before I pass the input to funcTwo I need to make an Ajax call to an endpoint passing the input and then I must pass the output produced by the AJAX call to funcTwo. funcTwo should be called only when the AJAX call is successful.

如何在NodeJS实现这一目标。我不知道如果能在这种情况下,可利用 Q库

How can I achieve this in NodeJS. I wonder if Q Library can be utilized in this case

推荐答案

function funcOne(input) { 
  var request = require('request');
  request.post(someUrl, {json: true, body: input}, function(err, res, body) {
      if (!err && res.statusCode === 200) {
          funcTwo(body, function(err, output) {
              console.log(err, output);
          });
      }
  });
}

function funcTwo(input, callback) {
    // process input
    callback(null, input);
}
阅读全文

相关推荐

最新文章