How can I do an X-HTTP-Method-Override for an ajax request in jQuery?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
回答2:
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...
}
});