如何使用 Meteor 从 API 导入 http 请求(JSON 格式)

How to import a http request (JSON format) from an API with Meteor

本文关键字:请求 JSON 格式 http 导入 何使用 Meteor API      更新时间:2023-09-26

我被困在Meteor上,我目前正在寻求从带有http请求的API导入数据。我通过 ajax 请求找到了答案,但 android 设备存在一些问题。

这是我在client/templates/categories/cat_list.js中的代码(该项目也在github上:https://github.com/balibou/wali):

感谢您的帮助:)(我尝试过使用"流星添加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>'
            );
        });
    },
});

我对你的回答是沟阿贾克斯!如果你想使用meteor,你必须使用Meteor 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); 
    }
});