当前位置: 代码迷 >> JavaScript >> 调用slack-hubot api和javascript _结果主体始终未定义
  详细解决方案

调用slack-hubot api和javascript _结果主体始终未定义

热度:36   发布时间:2023-06-13 12:31:10.0

但它总是返回未定义的主体。

这是我的源代码。 :静态变量

const TOKEN = 'xoxp-7186818662-7186899793-7811139362-ccc6df';
const emojiAPIUrl = 'https://slack.com/api/emoji.list';

:用途

module.exports = function (robot) {
    robot.hear(/show emoji/igm, function(msg){
        var paramData = {'token' : TOKEN};
        var result = robot.http(emojiAPIUrl)
                        .headers({'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/x-www-form-urlencoded'})
                        .post(paramData, function(error, res, body){
                            console.log('err >> ' + error);
                            console.log('res >> ' + stringifyObject(res));
                            console.log('body >> ' + body);
                        });
    });
};

以下是我得到的结果。

2015-07-19T06:22:27.303867+00:00 app[web.1]: err >> Error: socket hang up
2015-07-19T06:22:27.303960+00:00 app[web.1]: res >> 
2015-07-19T06:22:27.304014+00:00 app[web.1]: body >> undefined

您可以通过以下网址调用来检查结果

https://slack.com/api/emoji.list?token=xoxp-7186818662-7186899793-7811139362-ccc6df

我不明白为什么不能得到像url这样的json结果。 感谢:D

PS:如果您了解使用JavaScript的Hubot脚本指南,请共享:D。 网络上有很多示例,但大多数都是咖啡脚本,因此很难参考:-<

URL 内容不是POST而是GET,因此您应该执行以下操作:

robot.http("https://slack.com/api/emoji.list?token=xoxp-7186818662-7186899793-7811139362-ccc6df")
  .get()(function(err, res, body) {
     console.log(body);
  }
);
  相关解决方案