如何通过NodeJs中的异步调用发出POST请求

时间:2019-05-31 01:04:10

标签: node.js

我有以下路线

路线

{
    method: "POST",
    path: "/g/{gId}/exchange",
    config: {
      handler: eHandler ,
    }
  },

我的处理程序

const { pExchange } = require("../lib/gStore");

const eHandler = async (req, h) => {
  if(req.payload.card === undefined){
    return h.response("...").code(202);
  }
  if(req.params.gId === undefined){
    return h.response("...").code(202);
  }
  if (!pExchange (req.params.gId,req.state.player)) {
    return h.response("...").code(202);
  }

  return h.response("OK");
};

module.exports = eHandler ;

我想将其放入发布请求中,以便响应处于ajax调用中,而不要将我重定向到结果空白页。

1 个答案:

答案 0 :(得分:2)

尝试

const { pExchange } = require("../lib/gStore");

const eHandler = async (req, h) => {

let pExchangeValue = await pExchange(req.params.gId,req.state.player);

  if(req.payload.card === undefined){
    return h.response("...").code(202);
  }
  if(req.params.gId === undefined){
    return h.response("...").code(202);
  }
  if (!pExchangeValue) {
    return h.response("...").code(202);
  }

  return h.response("OK");
};

module.exports = eHandler ;