I have links like
http://example.com?query1=query1&query2=query2 as href of anchor tag.
I want to do post ajax on href click using data from the query parameter from the link.
I have written code
$("a").bind('click',function(){
$.ajax({type:'post',url:$(this).attr('href'), success: function(data){
alert(data);
}})
return false;
});
but it does not send query parameter inside ajax post request. Request query data length is 0.
how can I change this code to manipulate data from query parameter of link ?
Edit
Sorry my bad but it is not working these are the request
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Query String parameter
action:delete
id:12
So these fields are not going inside request content.
You need to use the value of the href attribute, not just the jQuery object itself.url:$(this).attr(‘href’).val()Update
Although you said this answer worked for you, I made a mistake. val is not a valid function of attr however doing it the original way you had it works for me.
See this demo: http://jsfiddle.net/PuyzU/
Update 2
Ok, I see your problem now. It wasn’t atall clear from the question what you meant.
Let me rewrite your question here to make sure I’m on the right track.
In your ajax post method there is a property
datawhere you’ll store the data you want to post to the server.In this case we can obtain the querysting parameter values as a single string by splitting our URL on the
?and use the first portion of the array as the URL and the second portion as the data we’re sending:Working Example