X-HTTP-Method-Override in jQuery?

*爱你&永不变心* 提交于 2019-12-03 03:13:43

问题


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...
    }
});


来源:https://stackoverflow.com/questions/1813156/x-http-method-override-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!