我是一个完整的新手,并尝试使用meteor创建一个社交网站聚合器,用户可以登录,发布网站,发表评论和评分。
该应用程序到目前为止功能,但是,我希望它能自动检索已发布网站的一些数据,即网站的标题和描述。
我已经完成了“meteor add http”并尝试构建它,但它并不总是有效。有时我得到的信息有时候没有,这很奇怪。有时我也会在控制台中收到以下消息“主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。有关更多帮助,请查看https://xhr.spec.whatwg.org/。”
任何人都可以帮忙解释为什么会这样吗?
谢谢
main.js
Template.website_form.events({
"click .js-toggle-website-form":function(event){
$("#website_form").toggle('slow');
},
"submit .js-save-website-form":function(event){
if (Meteor.user()) {
// here is an example of how to get the url out of the form:
var url = event.target.url.value;
console.log("The url they entered is: "+url);
// put your website saving code in here!
Meteor.call("getWebsiteData", url, function(error, results) {
var el = $('<div></div>');
el.html(results.content);
var title = $('title', el).text();
var description = $('meta[name="description"]',el).attr('content');
Websites.insert({
title: title,
url: url,
description: description,
createdOn: new Date(),
user: Meteor.user()._id,
up: 0,
down: 0
});
});
} else {
alert('You need to be logged in to submit websites!');
}
return false; // stop the form submit from reloading the page
}
});
startup.js
Meteor.methods({
getWebsiteData: function (url) {
this.unblock();
return Meteor.http.call("GET", url, {"npmRequestOptions" : {"gzip" : true}});
}
});