Thank you Robert. Your example teaches me how to extends the AJAX send.
juminrubin ( 2014-07-11 08:26:49 +0800 )edit-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Dear ZK Team,
What would be the best way to include a custom header on each request that will be send by zAu.send()?
Would it be a good idea to override the ajaxSendNow function in au.js?
function ajaxSendNow(reqInf) {
var setting = zAu.ajaxSettings,
req = setting.xhr(),
uri = zjq._useQS(reqInf) ? reqInf.uri + '?' + reqInf.content: null;
zAu.sentTime = jq.now();
try {
req.onreadystatechange = onResponseReady;
req.open("POST", uri ? uri: reqInf.uri, true);
req.setRequestHeader("Content-Type", setting.contentType);
req.setRequestHeader("ZK-SID", reqInf.sid);
req.setRequestHeader("MY_CUSTOM_HEADER", "TEST_VALUE");
Thank you.
Best regards,
yes you found the central point to hook into. However I would prefer not to override such a heavy method, even if it is possible.
Another way would be to override the zAu.ajaxSettings.xhr() function to create a custom XMLHttpRequest object, that will set the header automatically after calling the original open() function.
this example on ZK Fiddle shows how this can be done at page level.
it adds this script to a zul page:
<script>
zk.afterMount(function() {
var $xhr = zAu.ajaxSettings.xhr;
zAu.ajaxSettings.xhr = function() {
var req = $xhr.apply(this, arguments);
var $open = req.open;
req.open = function() {
$open.apply(req, arguments);
req.setRequestHeader('myheader', 'myvalue');
console.log("set custom header");
}
return req;
}
});
</script>
Regards,
Robert
Thank you Robert. Your example teaches me how to extends the AJAX send.
juminrubin ( 2014-07-11 08:26:49 +0800 )editAsked: 2014-07-10 13:05:42 +0800
Seen: 23 times
Last updated: Jul 10 '14