得到:$ . GET (. .)
职位:$ . POST () . .
PUT/DELETE呢?
得到:$ . GET (. .)
职位:$ . POST () . .
PUT/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
});
};
});
现在你可以使用:
$.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
console.log(result);
})
从这里复制
其他回答
这是一个更新的ajax调用,当你使用JSON与jQuery > 1.9:
$.ajax({
url: '/v1/object/3.json',
method: 'DELETE',
contentType: 'application/json',
success: function(result) {
// handle success
},
error: function(request,msg,error) {
// handle failure
}
});
简洁:
$.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
});
}
似乎可以通过JQuery的ajax函数指定
输入:“put”或 类型:“删除”
并不是所有浏览器都支持,而是大多数浏览器都支持。
看看这个问题,了解更多关于兼容性的信息:
PUT, DELETE, HEAD等方法在大多数网络浏览器中可用吗?
阿贾克斯()
寻找参数类型
这里也可以使用其他HTTP请求方法,如PUT和DELETE,但并非所有浏览器都支持它们。
你可以在你的数据散列中包含一个名为:_method的键,值为'delete'。
例如:
data = { id: 1, _method: 'delete' };
url = '/products'
request = $.post(url, data);
request.done(function(res){
alert('Yupi Yei. Your product has been deleted')
});
这也适用于