admin管理员组文章数量:1024347
I am using the following tag to let the users like the post
<a href="#" class="like" id="like_22">Like</a>
I get the id of the <a>
through Javascript which is actually the post_id and send the data to like.php
using the following GET
method.
post_id = 22;
xrequest.open("GET","like.php?post_id="+post_id+",true);
xrequest.send();
I have two questions
- How can I send this request using
POST
Method?
(Note that there is not any<form>...</form>
around the tag.) - If I use the above mentioned GET Method, Is it secure?
I am using the following tag to let the users like the post
<a href="#" class="like" id="like_22">Like</a>
I get the id of the <a>
through Javascript which is actually the post_id and send the data to like.php
using the following GET
method.
post_id = 22;
xrequest.open("GET","like.php?post_id="+post_id+",true);
xrequest.send();
I have two questions
- How can I send this request using
POST
Method?
(Note that there is not any<form>...</form>
around the tag.) - If I use the above mentioned GET Method, Is it secure?
3 Answers
Reset to default 6Since you've tagged jQuery, I assume you are using it. If so, you can do it like:
// 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;
});
Regarding your second question: No, it does not really make it safer unless you are doing it within SSL (https). a middle-man can still intercept your message mid-way and read the contents. POST makes it more indirect (to intercept and read the payload) than GET, but not safer.
To use POST to send the like link id you can do the following:
$(document).ready(function() {
$("a.like").click(function(event) {
var data = event.target.id;
$.ajax({
type: "POST",
url: "like.php",
data: data
});
});
});
updated, there was a typo in my code.
If i understand your question correctly, something like this should work for you:
$('#link_22').click(function(){
var link = $(this).attr('id');
$.post('/like.php/', {post_id: link});
return false;
})
Then you can reach this from PHP with something like:
$_POST['post_id'];
I am using the following tag to let the users like the post
<a href="#" class="like" id="like_22">Like</a>
I get the id of the <a>
through Javascript which is actually the post_id and send the data to like.php
using the following GET
method.
post_id = 22;
xrequest.open("GET","like.php?post_id="+post_id+",true);
xrequest.send();
I have two questions
- How can I send this request using
POST
Method?
(Note that there is not any<form>...</form>
around the tag.) - If I use the above mentioned GET Method, Is it secure?
I am using the following tag to let the users like the post
<a href="#" class="like" id="like_22">Like</a>
I get the id of the <a>
through Javascript which is actually the post_id and send the data to like.php
using the following GET
method.
post_id = 22;
xrequest.open("GET","like.php?post_id="+post_id+",true);
xrequest.send();
I have two questions
- How can I send this request using
POST
Method?
(Note that there is not any<form>...</form>
around the tag.) - If I use the above mentioned GET Method, Is it secure?
3 Answers
Reset to default 6Since you've tagged jQuery, I assume you are using it. If so, you can do it like:
// 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;
});
Regarding your second question: No, it does not really make it safer unless you are doing it within SSL (https). a middle-man can still intercept your message mid-way and read the contents. POST makes it more indirect (to intercept and read the payload) than GET, but not safer.
To use POST to send the like link id you can do the following:
$(document).ready(function() {
$("a.like").click(function(event) {
var data = event.target.id;
$.ajax({
type: "POST",
url: "like.php",
data: data
});
});
});
updated, there was a typo in my code.
If i understand your question correctly, something like this should work for you:
$('#link_22').click(function(){
var link = $(this).attr('id');
$.post('/like.php/', {post_id: link});
return false;
})
Then you can reach this from PHP with something like:
$_POST['post_id'];
本文标签: javascriptHow to use Html Anchor ltagt to send AJAX Post RequestStack Overflow
版权声明:本文标题:javascript - How to use Html Anchor <a> to send AJAX Post Request - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745519983a2154269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论