How to add a parameter to URL in all ajax calls?

2019-07-17 07:56发布

I have a Backbone application using jQuery and want to append a sessionId parameter to the URL of all ajax calls?

How can I do this in Backbone or jQuery, maybe in a global way and without rewriting the Backbone.Sync?

So if I have url www.example.com/users, I want that to become www.example.com/users?sessionId=12314q234123412

2条回答
淡お忘
2楼-- · 2019-07-17 08:07

You can use the global .ajaxSend() event.

查看更多
ら.Afraid
3楼-- · 2019-07-17 08:19

You can use the advanced options of jQuery.ajax. In your case, you should register a global prefilter to modify the sent data. Watch out to do this only on request to your own domain, otherwise that could be a security leakage.

Another option would be to create a simple wrapper function that extends the options passed in:

 function myAjax(options) {
     options.url = "mydomain";
     options.data = $.extend(options.data||{}, {sessionID: 12345});
     return $.ajax(options);
 }
查看更多
登录 后发表回答