gpt4 book ai didi

jquery - 获取 405 method not allowed 异常

转载 作者:行者123 更新时间:2023-12-01 01:23:31 25 4
gpt4 key购买 nike

我有一个 jquery 脚本(从 gi​​thub 下载)可以删除实体。以下是脚本。

$(document).ready(function() {


var restful = {

init: function(elem) {
elem.on('click', function(e) {
self=$(this);
e.preventDefault();

if(confirm('Are you sure you want to delete this record ? Note : The record will be deleted permanently from the database!')) {
$.ajax({
headers: {
Accept : "text/plain; charset=utf-8",
"Content-Type": "text/plain; charset=utf-8"
},
url: self.attr('href'),
method: 'DELETE',
success: function(data) {
self.closest('li').remove();
},
error: function(data) {
alert("Error while deleting.");
console.log(data);
}
});
}
})
}
};

restful.init($('.rest-delete'));

});

我就这样使用它

{{link_to_route('download.delete','x', ['id' => $download->id], array('class'=> 'rest-delete label label-danger')) }}

对应的laravel路由如下

Route::delete('/deletedownload/{id}', array('uses' => 'DownloadsController@deletedownload', 'as'=>'download.delete'));

但是,当我尝试按 X(删除按钮)时,出现 405 Method not allowed 错误。错误如下

DELETE http://production:1234/deletedownload/42 405 (Method Not Allowed) . 

这在我的本地沙箱上运行良好。

任何帮助将不胜感激。

谢谢

最佳答案

您已使用 method:DELETE 而不是在 ajax 调用中使用以下内容

$.ajax({
headers: {...},
url: self.attr('href'),
type:"post",
data: { _method:"DELETE" },
success: function(data) {...},
error: function(data) {...}
});

Laravel 将在 POST 中查找 _method,如果找到,将使用 DELETE 请求方法。

更新:( Because of this answer ,由 nietonfir 指向)

您可以像这样直接尝试DELETE方法(如果它不起作用,请尝试其他方法),:

$.ajax({
headers: {...},
url: self.attr('href'),
type:"DELETE",
success: function(data) {...},
error: function(data) {...}
});

关于jquery - 获取 405 method not allowed 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23898349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com