1

我们本地的 Hubot(“Sparky”)运行大量插件脚本,并且通常运行良好。我正在编写一个插件脚本,它对 Yahoo Pipes 进行 GET 调用,并期望 JSONP 作为结果。但是,我不确定该_callback参数使用什么。代码:

module.exports = (robot) ->
  robot.hear /\bkeyword\b/i, (msg) ->
    robot.http("http://pipes.yahoo.com/pipes/pipe.run")
      .query({
        _id: "legit-pipe-id-is-here",
        _render: "json",
        _callback: "?"
      })
      .get() (err, res, body) ->
        if body?
          data = JSON.parse(body)

这得到的错误是:

undefined:1
_({"count":10,"value":{"title":"correct title","description":"Pipes Output","lin
^
SyntaxError: Unexpected token _
  at Object.parse (native)
  at e:\node\sparky\scripts\plugin-name.coffee:26:11, <js>:11:23
  at IncomingMessage.<anonymous> (e:\node\sparky\node_modules\hubot\node_modules\scoped-http-client\lib\index.js:70:20)
  at IncomingMessage.EventEmitter.emit (events.js:117:20)
  at _stream_readable.js:920:16
  at process._tickCallback (node.js:415:13)

我已经验证管道在使用 jQuery 的 ajax 函数时可以正常工作,但在这种情况下,jQuery 会设置自己的回调。

4

1 回答 1

2

我刚刚意识到我不需要使用JSONP,所以我不需要_callback参数。当您不在浏览器中时,常规 JSON 可以正常工作,呵呵:

module.exports = (robot) ->
  robot.hear /\bkeyword\b/i, (msg) ->
    robot.http("http://pipes.yahoo.com/pipes/pipe.run")
      .query({
        _id: "legit-pipe-id-is-here",
        _render: "json"
      })
      .get() (err, res, body) ->
        if body?
          data = JSON.parse(body)
于 2014-08-29T16:13:07.850 回答