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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With