Zounadire
Zounadire

Reputation: 1564

How do I add parameters to every ajax request?

I'm developing a single page web-application using dojo and Java EE.

simplified architecture

The client scripts exchange data with the backend using the dojox.data.JsonRestStore.

I want to atach the session-ID to every AJAX-Request.

I know that you can achieve this in JQuery by using ajax setup

$.ajaxSetup({
    beforeSend: function() {
       //add parameters to request...
    }
});

Is there a way to setup dojo in a similar way?

Upvotes: 1

Views: 1117

Answers (3)

Dunaevsky Maxim
Dunaevsky Maxim

Reputation: 3199

You can for analogy jQuery.beforeSend catch Dojo XHR message "send" and modify request parameters:

require([
    "dojo/request/notify",
    "dojo/cookie"
], function (notify, cookie) {
    notify("send", function (response, cancel) {
        response.xhr.setRequestHeader('X-CSRFToken', cookie("csrftoken"));
    });
});

Upvotes: 0

mrtom
mrtom

Reputation: 2156

I don't think there's an explicit hook to enable this unfortunately. One way to get around this (not tested, and it's not too pretty, but should do what you want):

define("my.xhr_fixer", ["dojo/xhr"], function(dojo){

(function() {
    dojo._xhr_orig = dojo.xhr;
    dojo.xhr = function(/*String*/ method, /*dojo.__XhrArgs*/ args, /*Boolean?*/ hasBody){
        args = args || {};
        args.content = args.content || {};

        args.content['sessionId'] = mySessionId;

        return dojo._xhr_orig.apply(this, arguments);
    }
})();
});

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114347

Put it in the URL:

$.ajaxSetup({
    url: "/yourcode?sessionId=" + sessionId;
});

Upvotes: 0

Related Questions