简体   繁体   中英

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

I'm stuck with Meteor, i'm currently looking to import data from an API with a http request. I found the answer with an ajax request but there's some issues with an android device.

Here is my code in client/templates/categories/cat_list.js (the project is also on github: https://github.com/balibou/wali ):

Thx for the help :) (i've tried with the "meteor add http" but it was really a mess ...)

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>'
            );
        });
    },
});

My answer to you is DITCH Ajax! You have to use Meteor HTTP if you want to use meteor... http://docs.meteor.com/#/full/http_call read the docs be familier with it. Also you need to choose if you want to use Client side or Server side call. And here is some HTTP function to get you started:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM