【问题标题】:send json file using http.post meteor and then get it via http.get使用 http.post 流星发送 json 文件,然后通过 http.get 获取它
【发布时间】:2017-08-16 11:14:41
【问题描述】:

我需要使用 http.post 将 json 格式的数据发送到 url。写了以下代码在本地测试

 var postData = {
"channelName" : "Number Theory1",
"startDate" : "2017-07-22T06:29:35.681Z",
"endDate" : "2017-08-22T06:29:35.681Z"
}
HTTP.call('POST', 'http://localhost:3000', {
   data: postData 
 }, (error, result) => {
 if (error) {
   console.log('we are getting this error:' + error);
 } else {
    console.log('POstres : ' + result);
 }
 }); 
function extractProcessData(data){
   console.log('function called! : ' + data);     ##data here should be var processData but it prints undefined
}
function confirmDataReceived(data) {
  HTTP.get('http://localhost:3000', function(err, res){
  // confirmation error
  if(err){
    console.log('error ' + err);
  }
  // confirmation success and process data
  else{
    console.log('data : ' + data + res)    ## data here prints as undefined whereas according to me, it should contain thevar postData ##
    extractProcessData(data) //call function to process data
  }
});
}
var postRoutes = Picker.filter(Meteor.bindEnvironment(function(req, res) {
 if (req.method == "POST"){
   console.log('req : ' + req.method + " " + req.body)
   confirmDataReceived(req.body);
 }
 return true;
 // return req.method == "POST";
}));;;

它不工作。我想问题出在 http.post 数据语法上。它是通过http.post发送数据的正确方法吗?我希望我通过选择器功能的 http.get 方法是正确的。

任何帮助将不胜感激。

干杯!

【问题讨论】:

    标签: web-services meteor http-post


    【解决方案1】:
    var postData = {
        "channelName" : "Number Theory1",
        "startDate" : "2017-07-22T06:29:35.681Z",
        "endDate" : "2017-08-22T06:29:35.681Z"
    }
    HTTP.call('POST', 'http://localhost:3000', {
      data: postData 
    }, (error, result) => {
      if (error) {
           console.log('we are getting this error:' + error);
      } else {
            console.log('POstres : ' + JSON.stringify(result));
      }
    }); 
    

    【讨论】:

    • 不工作。发布整个场景以便更好地理解
    • 为什么,我的代码中有与此相关的错误吗? afaik,我的路线和依赖项是正确的
    • 你检查/调用了你编码http调用的函数吗?
    • 所有代码都在服务器端。服务器一启动,就会触发 http.call.post
    猜你喜欢
    • 2018-01-20
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多