I’m having trouble figuring out how to hook in data each time jQuerys $.post() method is called.
I have several event handlers that obtains values from throughout the page before submitting them to the server via AJAX. An example of such a call is as follows:
var data = {
link_data:{'foo': 'bar', 'foo2' : 'bar2'}
};
$.post('some_url',data,function(returned_data){
//Do something with returned_data
});
However, I was wondering if there was a way of hooking in an additional piece of data that is sent with every $.post() request made.
Essentially, what I was hoping for was a way to amend the ‘data’ parameter that’s sent to the server just before submission so that the sent value looks something more like this:
{
token: some_predefined_variable,
link_data{'foo': 'bar', 'foo2' : 'bar2'}
}
Is there a way to hook this in so that I don’t have to go and amend every single method where data is being sent to the server by $.post()?
Thanks.
Update: I found the best solution was actually posted here. Bit annoying that you have to manually set the post data but it works 🙂
You can use
$.ajaxSetup()andbeforeSend