如何在 jQuery 中发送 PUT/DELETE 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2153917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a PUT/DELETE request in jQuery?
提问by user198729
GET
:$.get(..)
GET
:$.get(..)
POST
:$.post()..
POST
:$.post()..
What about PUT/DELETE
?
怎么样PUT/DELETE
?
回答by Darin Dimitrov
回答by Jacob Relkin
回答by Stepan Suvorov
We can extend jQuery to make shortcuts for PUT and DELETE:
我们可以扩展 jQuery 来制作 PUT 和 DELETE 的快捷方式:
jQuery.each( [ "put", "delete" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}
return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callback
});
};
});
and now you can use:
现在你可以使用:
$.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
console.log(result);
})
copy from here
从这里复制
回答by Pekka
Seems to be possible with JQuery's ajax functionby specifying
似乎可以通过指定使用JQuery 的 ajax 函数
type: "put"
or
type: "delete"
type: "put"
或者
type: "delete"
and is not not supported by all browsers, but most of them.
并不是所有浏览器都支持,但大多数浏览器都支持。
Check out this question for more info on compatibility:
查看此问题以获取有关兼容性的更多信息:
Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
回答by user2503775
From here, you can do this:
从这里,您可以执行以下操作:
/* Extend jQuery with functions for PUT and DELETE requests. */
function _ajax_request(url, data, callback, type, method) {
if (jQuery.isFunction(data)) {
callback = data;
data = {};
}
return jQuery.ajax({
type: method,
url: url,
data: data,
success: callback,
dataType: type
});
}
jQuery.extend({
put: function(url, data, callback, type) {
return _ajax_request(url, data, callback, type, 'PUT');
},
delete_: function(url, data, callback, type) {
return _ajax_request(url, data, callback, type, 'DELETE');
}
});
It's basically just a copy of $.post()
with the method parameter adapted.
它基本上只是$.post()
修改了方法参数的副本。
回答by moodboom
Here's an updated ajaxcall for when you are using JSON with jQuery > 1.9:
当您在 jQuery > 1.9 中使用 JSON 时,这是一个更新的ajax调用:
$.ajax({
url: '/v1/object/3.json',
method: 'DELETE',
contentType: 'application/json',
success: function(result) {
// handle success
},
error: function(request,msg,error) {
// handle failure
}
});
回答by Pascal MARTIN
You should be able to use jQuery.ajax
:
您应该能够使用jQuery.ajax
:
Load a remote page using an HTTP request.
使用 HTTP 请求加载远程页面。
And you can specify which method should be used, with the type
option:
您可以使用type
选项指定应使用哪种方法:
The type of request to make ("
POST
" or "GET
"), default is "GET
".
Note: Other HTTP request methods, such asPUT
andDELETE
, can also be used here, but they are not supported by all browsers.
要发出的请求类型("
POST
" 或 "GET
"),默认为 "GET
"。
注意:这里也可以使用其他 HTTP 请求方法,例如PUT
和DELETE
,但并非所有浏览器都支持。
回答by antpaw
回答by Paul Wand
For brevity:
为简洁起见:
$.delete = function(url, data, callback, type){
if ( $.isFunction(data) ){
type = type || callback,
callback = data,
data = {}
}
return $.ajax({
url: url,
type: 'DELETE',
success: callback,
data: data,
contentType: type
});
}
回答by Xanarus
You can do it with AJAX !
你可以用 AJAX 做到这一点!
For PUT
method :
对于PUT
方法:
$.ajax({
url: 'path.php',
type: 'PUT',
success: function(data) {
//play with data
}
});
For DELETE
method :
对于DELETE
方法:
$.ajax({
url: 'path.php',
type: 'DELETE',
success: function(data) {
//play with data
}
});