我有一个博客网站托管在 Windows Sever、ASP.Net 3.5、ASP.Net AJAX、SQL Server 后台。
我想为博主提供一个像“digg-it”这样的按钮,他们可以将其放在自己的博客上,以便读者如果喜欢该帖子,可以点击该帖子来点赞。
我知道我将使用 Javascript 来做到这一点。我能做什么:-
- 从我的网站检索代码,该代码将显示当前点赞数。
- 如果用户点赞,则增加我网站上的计数。
由于大多数博客都位于 blogger.com/wordpress.com 上,因此插件代码将嵌入博客主题中。我想我会使用博客文章的 URL 作为唯一 ID。我的问题是如何让我的网站和 blogger.com 上的 javascript 进行交互。
我们将不胜感激您的帮助。
谢谢
最佳答案
您可以考虑使用 SCRIPT 回调加载 JSON 数据,而不是使用 XmlHttpRequest 来解决跨域问题。
function dynScript(url){
var script=document.createElement('script');
script.src=url;
script.type="text/javascript";
document.getElementsByTagName('head')[0].appendChild(script);
}
function handleYourData(json) {
// Do something with your response data if you need to, like alter
// dom.
}
function thumbUp(postId) {
dynScript('http://yourdomain.com/path/to/thumbHandler?callback=handleYourData&thumbs=up&postId=' + postId);
}
function thumbDown(postId) {
dynScript('http://yourdomain.com/path/to/thumbHandler?callback=handleYourData&thumbs=down&postId=' + postId);
}
您可以在 HTML 中像这样使用它。
<a onClick="thumbUp(521);">Thumb up</a> | <a onClick="thumbDown(521);">Thumb Down</a>
您的 thumbHandler
代码必须输出包含 handleYourData()
的 JSON,以便以 JSON 数据作为参数调用您的回调。
关于.net - 如何为我的网站制作一个像 'Digg it' 这样的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/410820/