Setting headers when using jQuery Form Plugin
Asked Answered
J

2

3

I'm using JQuery Form Plugin and I'm not sure how to set headers.

I want to be able accomplish something like this:

$.ajax({
                url: "/url",
                data: post,
                type: "POST",
                beforeSend: function(jqXHR) {
                    jqXHR.setRequestHeader("foo", "bar"); 
                },
})

but the beforeSubmit event dosn't pass the jqXHR object for manipulation. Any ideas?

Jaymejaymee answered 29/9, 2011 at 12:59 Comment(0)
J
3

As it turns out, the options object you send to the form plugin actually passes the options to the $.ajax method. Therefore you can use the native before send function.

Jaymejaymee answered 29/9, 2011 at 14:3 Comment(1)
Here's an example to set the header with the request using the jquery ajax form plugin beforeSubmit function: options.headers = {'X-Csrf-Token': window.Cookies.get('_csrf')}Vachil
C
6

The following code works for me.

$('#myForm').ajaxSubmit({
    headers: {
        "foo": "bar"
    }
});
Contrastive answered 12/8, 2015 at 3:26 Comment(0)
J
3

As it turns out, the options object you send to the form plugin actually passes the options to the $.ajax method. Therefore you can use the native before send function.

Jaymejaymee answered 29/9, 2011 at 14:3 Comment(1)
Here's an example to set the header with the request using the jquery ajax form plugin beforeSubmit function: options.headers = {'X-Csrf-Token': window.Cookies.get('_csrf')}Vachil

© 2022 - 2024 — McMap. All rights reserved.