简体   繁体   中英

In JQuery/Backbone, how can I override every AJAX request to add an additional parameter?

$.post('/js/register', data, function(){

});

$.get('/js/home', data, function(){
    //render the template here
});

I use these in my Backbone app. It fetches the data from the server, then renders the template.

I want to override these on a global level so that when I make these calls, "data" has the current window.location (the url) as an attribute.

For EVERY ajax call, I want to append the current url that the user is currently on.

PS - I'm using Backbone push state - does that make a difference?

I think this answer should help you out https://stackoverflow.com/a/6998085/1846192

In your case:

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
  options.data = $.param($.extend(originalOptions.data, { url: window.location.href }));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM