gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:48:29 24 4
gpt4 key购买 nike

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

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

感谢您的帮助:)(我尝试过“meteor add 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>'
);
});
},
});

最佳答案

我对你的回答是放弃 Ajax!如果你想使用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);
}
});

关于javascript - 如何使用 Meteor 从 API 导入 http 请求(JSON 格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com