如何使通过NodeJS到端点Ajax请求 [英] How to make Ajax request through NodeJS to an endpoint

查看:153
本文介绍了如何使通过NodeJS到端点Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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);
}

这篇关于如何使通过NodeJS到端点Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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