我使用以下标记让用户喜欢帖子
<a href="#" class="like" id="like_22">Like</a>
我通过Javascript获取了<a>
的ID,这实际上是post_id,并使用以下like.php
方法将数据发送到GET
。
post_id = 22;
xrequest.open("GET","like.php?post_id="+post_id+",true);
xrequest.send();
我有两个问题
POST
方法发送此请求?<form>...</form>
。)答案 0 :(得分:5)
由于您已经标记了jQuery,我假设您正在使用它。如果是这样,你可以这样做:
// attach a click handler for all links with class "like"
$('a.like').click(function() {
// use jQuery's $.post, to send the request
// the second argument is the request data
$.post('like.php', {post_id: this.id}, function(data) {
// data is what your server returns
});
// prevent the link's default behavior
return false;
});
关于您的第二个问题:否,除非您在SSL(https)中执行此操作,否则它并不会使其更安全。中间人仍然可以在中途拦截你的信息并阅读内容。与GET相比,POST使其更加间接(拦截和读取有效载荷),但不是更安全。
答案 1 :(得分:1)
要使用POST发送类似的链接ID,您可以执行以下操作:
$(document).ready(function() {
$("a.like").click(function(event) {
var data = event.target.id;
$.ajax({
type: "POST",
url: "like.php",
data: data
});
});
});
更新了,我的代码中有一个拼写错误。
答案 2 :(得分:0)
如果我理解你的问题,这样的事情对你有用:
$('#link_22').click(function(){
var link = $(this).attr('id');
$.post('/like.php/', {post_id: link});
return false;
})
然后你可以通过PHP来达到这个目的:
$_POST['post_id'];