I have url values in my client-side script. While looping over them, I want to create links that will pass these URL values on-click back to my server-side click handler. The server-side code (/logclick
) records the time and destination of the click for audit purposes before the user is directed to the url location that was passed开发者_如何转开发 as a parameter to the /logclick
server-side handler.
$.each(data.items, function(i, item)
{
$('#link').append("<a href='" + item.url + "'></a>");
});
The question is how to write the link so that the "virtual link" passes the destination URL to the server-side code without misinterpreting the destination URL as part of the "virtual link"?
What I mean is, if the "virtual link" looks like this:
/logclick/http://www.google.com/reader/view/subscription/a
then the server-side code will complain that it doesn't recognize this destination (it will think the google.com destination is part of the /logclick
url-route instead of a string value that is being passed to the /logclick
route.
But, in the context of a jQuery $.each
loop, how would I create URLs that POST
values to the /logclick
handler rather than GET
?
I'm not sure what your server-side framework is doing, but you may need to encode that part of the URL.
$('#link').append("<a href='" + encodeURIComponent(item.url) + "'></a>");
But, in the context of a jQuery $.each loop, how would I create URLs that POST values to the /logclick handler rather than GET?
In order to send a POST request you need to use existing FORM element for that.
<form id="my-post-form" method="post" action="/uri-router/">
<input type="hidden" name="item-url" value="" /></form>
...
$.each(data.items, function(i, item)
{
var link = $("<a />");
link.attr('href', item.url).click(function(){
$('#my-post-form input[name=item-url]').val($(this).attr('href'));
$('#my-post-form')[0].submit();
return false;
});
$('#link').append(link);
});
精彩评论