本文介绍了回送后方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送带有回送"invokeStaticMethod"的发布请求.请帮我怎么做.我想向以下网址发送 POST API 请求:

I want to send a post request with loopback "invokeStaticMethod".Please help me how to do it.I want to send a POST API request to below url:

本地主机:3000/api/user/id/unblock ,使用参数 {"userId","blockId"}

请让我知道如何通过回送发送POST请求

Please let me know how can I send a POST request with Loopback

推荐答案

您可以创建像这样的远程方法:

You could create a remote method like this:

User.unblock = function(id, userId, blockId, callback) {
  var result;
  // TODO
  callback(null, result);
};

然后,json文件中的远程方法定义如下所示:

Then, the remote method definition in the json file could look like this:

"unblock": {
  "accepts": [
    {
      "arg": "id",
      "type": "string",
      "required": true,
      "description": "",
      "http": {
        "source": "path"
      }
    },
    {
      "arg": "userId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    },
    {
      "arg": "blockId",
      "type": "string",
      "required": false,
      "description": "",
      "http": {
        "source": "form"
      }
    }
  ],
  "returns": [
    {
      "arg": "result",
      "type": "object",
      "root": false,
      "description": ""
    }
  ],
  "description": "",
  "http": [
    {
      "path": "/:id/unblock",
      "verb": "post"
    }
  ]
}

然后,您的远程方法将如下所示:

Then your remote method would look like this:

您可以使用函数参数,并使用一个body参数而不是2个form参数,并从那里读取数据,尽管我相信,如果只有2个其他参数,最好将它们分开放置.但这取决于您的方法.

You could play around with function arguments and use one body argument instead of 2 form arguments and read the data from there, although I believe that if there are only 2 additional parameters it's better to put them separately. But it depends on your approach.

这篇关于回送后方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:38