Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YUI 3 - Set global request headers for Ajax

I've mostly worked with jQuery before and I'm new to YUI. I wish to set a custom header for each Ajax request using either IO or DataSource in YUI 3. I want the header to be inserted automatically for each request. In jQuery I could accomplish this with $.ajaxPrefilter like so:

$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    var value = 'blah';
    if (value) {
        jqXHR.setRequestHeader("My-Custom-Header", value);
    }
});

I found these pages in the online documentation for YUI 3 but I just don't "get it". How can I accomplish this?

http://developer.yahoo.com/yui/3/examples/io/io-get.html

http://developer.yahoo.com/yui/3/api/io.html

like image 455
Lucius McLovin Avatar asked Feb 24 '23 16:02

Lucius McLovin


1 Answers

Check out the "header" method in the io module: API docs

I haven't tested it, but you should be able to do something like this:

YUI().use('io', function(Y) {
    Y.io.header('X-My-Header', 'My Custom Value');

    Y.io(/*...*/); // Should have the X-My-Header HTTP header
});

Note that this will only apply to the current YUI instance. So if you have another YUI().use(/.../) statement, you'll need to set the header again.

If you need it to provide headers across instances, you should define your own module that wraps the Y.io functionality. Check out this gist to get a sense of what that entails.

like image 160
Nick Husher Avatar answered Mar 06 '23 18:03

Nick Husher