2014-11-22 77 views
1

我困在Meteor中,我目前正在尋找使用http請求從API導入數據。 我找到了答案與ajax請求,但有一些與Android設備的問題。如何使用Meteor從API導入http請求(JSON格式)

這是我在客戶端/模板/分類/ cat_list.js代碼(該項目也是在GitHub上:https://github.com/balibou/wali):

Thx的幫助:)(我試過用「流星添加http 「但它真的是一團糟......)

Template.catList.helpers({ 
    categories: function() { 
     return Categories.find(); 
    } 
}); 

Template.catList.events({ 
    "click .toggle-checked": function() { 
     // Set the checked property to the opposite of its current value 
     Categories.update(this._id, {$set: {checked: ! this.checked}}); 

     var jsonData = ''+ 
     '{"ApiKey": "544bf635-7f4c-4fb5-9fbe-88116a2dddd5", '+ 
     ' "SearchRequest": {        '+ 
     '  "Keyword": "'+ this.title + '",    '+ 
     '  "SortBy": "relevance",      '+ 
     '  "Pagination": {        '+ 
     '   "ItemsPerPage": 5,      '+ 
     '   "PageNumber": 0       '+ 
     '  },           '+ 
     '  "Filters": {        '+ 
     '   "Price": {        '+ 
     '    "Min": 0,       '+ 
     '    "Max": 400       '+ 
     '   },          '+ 
     '   "Navigation": "computers",    '+ 
     '   "IncludeMarketPlace": false,   '+ 
     '   "Brands": [ "asus" ],     '+ 
     '   "Condition": null      '+ 
     '  }           '+ 
     ' }            '+ 
     '}             '; 
     console.log(this.title); 
     $.ajax({ 
      type: "POST", 
      url: "https://api.cdiscount.com/OpenApi/json/Search", 
      data: jsonData 
     }).done(function(msg) { 
      console.log(msg) 
      $("#results").html(
       '<div class="product">'+ 
       ' <h3>'+msg.Products[0].Name+'</h3>'+ 
       ' <img src="'+msg.Products[0].MainImageUrl+'">'+ 
       '</div>' 
      ); 
     }); 
    }, 
}); 

回答

1

我對你的回答是DITCH Ajax!如果你想使用流星,你必須使用流星HTTP ... http://docs.meteor.com/#/full/http_call閱讀文檔與它有家庭關係。您還需要選擇是否要使用客戶端或服務器端呼叫。這裏有一些HTTP功能讓你開始:

Template.catList.events({ 
    "click .toggle-checked": function() { 
     HTTP.call("POST", "https://api.cdiscount.com/OpenApi/json/Search", jsonData); 
    } 
}); 
+0

好吧謝謝你,我會用這段代碼弄清楚它。 – Benjamin 2014-11-23 10:52:36