Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X-HTTP-Method-Override in jQuery?

How can I do an X-HTTP-Method-Override for an ajax request in jQuery?

like image 413
coolaj86 Avatar asked Nov 28 '09 17:11

coolaj86


People also ask

How do you use override in X HTTP?

For tighter security, some firewalls do not allow HTTP PUT or DELETE traffic. To accommodate this restriction, you can send these requests in two ways: Use the X-Method-Override or X-HTTP-Method-Override HTTP header fields to channel a PUT or DELETE request through a POST request.

What is X HTTP method?

To address this need, the X-HTTP-Method header can be added to a POST request that signals that the server MUST process the request not as a POST, but as if the HTTP verb specified as the value of the header was used as the method on the HTTP request's request line, as specified in [RFC2616] section 5.1.


2 Answers

With 1.5 you can now pass in a headers option:

$.ajax({
  headers: {
    'X-HTTP-Method-Override': 'DELETE'
  },
  method: 'GET'
  // more parameters...
});

This is set before 'beforeSend' is called, so it could still get overwritten. See http://api.jquery.com/jQuery.ajax/

-- fixed incorrect syntax (wouldn't let me save with less than 6 character edit, so writing this message

like image 91
scurker Avatar answered Oct 05 '22 13:10

scurker


You could set custom headers when performing an ajax request by using the beforeSend callback:

$.ajax({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('X-HTTP-Method-Override', 'PUT');
    },
    type: 'POST',
    url: '/someurl',
    success: function(data){
        // do something...
    }
});
like image 29
Darin Dimitrov Avatar answered Oct 05 '22 13:10

Darin Dimitrov