2

I'm using ajaxSend() to intercept requests originating from a jQuery plugin on my page and then filtering irrelevant requests using the ajaxOptions url.

$('some-selector').ajaxSend(function(e, xhr, settings) {
    var ajaxUrl = settings.url;
    if (ajaxUrl.search('targetpage.aspx') == 0) {
         //need to add an additional parameter to my request here
    } 
}

I need to insert an additional parameter into relevant requests. Is it possible to modify the request at this point?

Noel Sequeira
  • 21
  • 1
  • 2

3 Answers3

4

You could append the parameter to the settings.data hash by checking that it is not null before:

if (settings.data == null) {
    settings.data = { };
}
settings.data['param'] = 'value';

or just modify the url:

settings.url += (settings.url.match(/\?/) ? "&" : "?") + 'param=value';
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Darin, I tried appending my parameter to settings.url (without success) before I posted the question. While I used a workaround to solve my initial problem (I hooked into the plugin to modify the url before the request is sent), I'd still like to know why I'm drawing a blank with this method. – Noel Sequeira Feb 25 '10 at 07:43
  • 1
    this doesn't work because data in ajaxSend is a query string not an object, you should make something like: if(settings.data) settings.data += "&"; settings.data += "param=value"; – Enrique Jan 14 '12 at 17:11
2

This is an old question, but I came here from Google and thought other people searching the same thing should have a working solution. I have tried the suggested answer but it does not work. Modifying the data in ajaxSend handler does not change what is being sent.

My objective was the same as OP's, insert an additional parameter into the requests.

So I devised a hacky solution for it: override jQuery's ajax method. Just replace "injected_param" and "injected_value" to suit your needs. I've added some guards to check whether the parameter is already present or not, just in case.

$(function() {

        var jqAjax = $.ajax;
        $.ajax = function (settings, settingsObj) {

            if (typeof(settings) == "string") {
                settingsObj.url = settings;
                settings = settingsObj;
            }

            if (!settings.data) { 
                settings.data = {};
            }

            if (typeof(settings.data) == "string" && settings.data.indexOf("&injected_param=") < 0) {
                settings.data += "&injected_param=injected_value";
            }

            else if (!settings.data.injected_param) {
                settings.data.injected_param = "injected_value";
            }


            jqAjax (settings);
        };
    });
  • Thought not recommended by the jQuery docs, it would be safer to implement $.ajaxSetup() (http://api.jquery.com/jQuery.ajaxSetup/) to intercept outgoing AJAX requests in order to modify them rather than overriding a core jQuery function. – Glen Selle Jun 09 '14 at 22:08
  • Hello, @Glen! I can't remember the exact circunstances of this code, as it is from 2 years ago, but from what I see from the docs you cited, `ajaxSetup` allows me to set default values for `$.ajax` calls, not to add new data for the request ontop of the data supplied to an `$.ajax` call. The best candidate for this functionality was `ajaxSend`, and (2 years ago) it didn't work for modifying the data for an ajax request, so I had to devise this hack. I don't know if it is a bug, or by design, and if it is working on most recent jQuery versions, but that was the solution at the time. – NothingsImpossible Jun 10 '14 at 02:24
0

Or:

//Interrupt AJAX requests and switch out the URL.
jQuery(document).bind('ajaxSend', function(event, xhr, params) {

    event.preventDefault();

    //Make sure this isn't one we've already modified, or it will loop infinitely
    if (!~params.url.search("http://www.example.com")) {

        //This is an untainted url, so change it.
        params.url = "http://www.example.com";

        //Send the request again with the new parameters.
        jQuery.ajax(params);
    }

    return false;
});
Chad Hedgcock
  • 11,125
  • 3
  • 36
  • 44