Answers:
您可以使用ajax方法:
$.ajax({
url: '/script.cgi',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
PUT
或DELETE
请求返回了404错误,则需要在IIS中启用这些动词。:我发现这是一个很好的资源geekswithblogs.net/michelotti/archive/2011/05/28/...
"The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers."
来自:api.jquery.com/jQuery.ajax/#options
method
或type
我们可以扩展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);
})
从这里复制
在这里,您可以执行以下操作:
/* 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');
}
});
它基本上只是$.post()
方法参数经过修改的的一个副本。
如果您需要对$.post
Laravel进行处理,Route::delete
或者Route::put
只是添加一个参数"_method"="delete"
或"_method"="put"
。
$.post("your/uri/here", {"arg1":"value1",...,"_method":"delete"}, function(data){}); ...
必须为其他人工作
注意:已通过Laravel 5.6和jQuery 3测试
您可以在数据哈希中包含一个名为:_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')
});
这也将适用于
这是一个简单的单行代码,我用来放置多个变量:
$.put("https://your-url.com",{item1:'new item1',item2:'new items2'});