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({
beforeS开发者_如何学Cend: function() {
//add parameters to request...
}
});
Is there a way to setup dojo in a similar way?
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);
}
})();
});
Put it in the URL:
$.ajaxSetup({
url: "/yourcode?sessionId=" + sessionId;
});
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"));
});
});
精彩评论