【问题标题】:Why is my HTTP.call() not working?为什么我的 HTTP.call() 不起作用?
【发布时间】:2017-07-13 04:54:54
【问题描述】:

为什么我的 HTTP.call() 函数没有按设计工作。

当我在浏览器控制台中运行以下代码时:

var postData = {
data: {

  "username": "SirBT",
  "productName": "Choma",
  "phoneNumber": "+254700087633",
  "currencyCode": "KES",
  "amount": "666"      
  }

}

HTTP.call('POST', ''http://2117c3a8.ngrok.io/AConnect/c2b.php', 
{ headers: { apiKey: 'AC6ea8e1541e7ee0c40915e' }}, 
  postData, 
  function(error, result) {
    if (error) {
        console.log(error);
    }
    if (result) {
        console.log(result);
    }
} 
);

我收到此错误消息:

Uncaught Error: Can't make a blocking HTTP call from the client; callback required.(…)

经过一番研究,我发现我应该从服务器运行它,我现在这样做了,但是我在终端中收到了这些错误消息:

W20170712-17:31:47.726(3)? (STDERR)     /home/sirbt/escrow/.meteor/local/build/programs/server/packages/http.js:187
W20170712-17:31:47.728(3)? (STDERR)       callback(error, response);                                                                                       // 74
W20170712-17:31:47.732(3)? (STDERR)       ^
W20170712-17:31:47.733(3)? (STDERR) 
W20170712-17:31:47.735(3)? (STDERR) TypeError: callback is not a function
W20170712-17:31:47.736(3)? (STDERR)     at     packages/http/httpcall_server.js:74:7
W20170712-17:31:47.737(3)? (STDERR)     at   packages/underscore.js:784:19
W20170712-17:31:47.738(3)? (STDERR)     at Request._callback (packages/http/httpcall_server.js:116:5)
W20170712-17:31:47.740(3)? (STDERR)     at Request.self.callback (/home/sirbt/.meteor/packages/http/.1.2.12.1w7h08f++os+web.browser+web.cordova/npm/node_modules/request/request.js:200:22)
W20170712-17:31:47.741(3)? (STDERR)     at emitTwo (events.js:87:13)
W20170712-17:31:47.742(3)? (STDERR)     at Request.emit (events.js:172:7)
W20170712-17:31:47.743(3)? (STDERR)     at Request.<anonymous> (/home/sirbt/.meteor/packages/http/.1.2.12.1w7h08f++os+web.browser+web.cordova/npm/node_modules/request/request.js:1067:10)
W20170712-17:31:47.745(3)? (STDERR)     at emitOne (events.js:82:20)
W20170712-17:31:47.746(3)? (STDERR)     at Request.emit (events.js:169:7)
W20170712-17:31:47.747(3)? (STDERR)     at IncomingMessage.<anonymous> (/home/sirbt/.meteor/packages/http/.1.2.12.1w7h08f++os+web.browser+web.cordova/npm/node_modules/request/request.js:988:12)
=> Exited with code: 1

期待您的帮助

【问题讨论】:

  • 答案反映在错误信息中。你应该使用“回调”

标签: post meteor callback xmlhttprequest


【解决方案1】:

你有 5 个参数,而 docs write 你需要 4 个。

在我的 sn-p 中检查postData 字段的变化情况,它与headers 在同一个对象中

HTTP.call('POST', 'http://2117c3a8.ngrok.io/AConnect/c2b.php', { 
  headers: { apiKey: 'AC6ea8e1541e7ee0c40915e' }, 
  data: postData
}, function(error, result) {
    if (error) {
        console.log(error);
    }
    if (result) {
        console.log(result);
    }
});

【讨论】:

猜你喜欢
  • 2015-09-25
  • 2018-03-05
  • 2013-04-19
  • 2015-10-17
  • 2016-02-20
  • 2023-04-03
  • 2012-01-07
  • 2012-06-11
  • 2015-08-26
相关资源
最近更新 更多