简体   繁体   中英

Add HTTP Header to Wicket Ajax Request

I need to add a HTTP header to all Ajax (XHR) Requests of Wicket. I've tried the following:

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('X-My-Header', 'value');
    }
});

and

$(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader('X-My-Header', 'value');
});

It doesn't work.

What did I wrong?

How can I solve this?

SOLUTION

Wicket uses it's own stuff to register global listeners.

Wicket.Event.subscribe('/ajax/call/beforeSend', function(jqEvent, attributes, jqXHR, errorThrown, textStatus) {
    jqXHR.setRequestHeader('X-My-Header', 'value');
});

I don't know why your attempts failed, but wicket provides support for such requirements with AJAX global listeners. Search for paragraph 'Global listener' in this chapter:

https://ci.apache.org/projects/wicket/guide/7.x/guide/ajax.html#ajax_6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM