我正在尝试使用ajax从文件夹中删除图像,并且正在使用delete方法在路由中删除。在控制器中,试图使用laravel中的图像名称来删除图像。
路线:
Route::delete('remove-social/{filename}', 'Webadmin\Socials@removesocial');
控制器:
public function removesocial($filename){
File::delete('public/assets/uploads/Social/' . $filename);
}
查看:
<a href="javascript:removesocialimage()" style="color: white;text-decoration: none;" class="btn btn-red">
<i class="glyphicon glyphicon-trash "></i> Remove</a> </label>
<script>
function removesocialimage() {
if (j('#file_name').val() != '')
if (confirm('Are you sure want to remove social icon?')) {
j('#loading').css('display', 'block');
var form_data = new FormData();
form_data.append('_method', 'DELETE');
form_data.append('_token', '{{csrf_token()}}');
j.ajax({
url: "remove-social/" + j('#file_name').val(),
data: form_data,
type: 'POST',
contentType: false,
processData: false,
success: function (data) {
j('#preview_image').attr('src', '{{URL::to('/public/assets/Webadmin/images/attach-1.png')}}');
j('#file_name').val('');
j('#loading').css('display', 'none');
},
error: function (xhr, status, error) {
alert(error);
alert(xhr.responseText);
}
});
}
}
</script>
答案 0 :(得分:1)
将此包含在您的视图标题中:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
并在进行网络调用之前执行以下ajax设置:
linkText
答案 1 :(得分:0)
尝试将路线delete
更改为post
的方法
Route::post('remove-social/{filename}', 'Webadmin\Socials@removesocial');
如果您想使用删除方法,那么您的ajax看起来就像
添加头部html标签
<meta name="csrf-token" content="{{ csrf_token() }}">
js代码
j.ajax({
url: "remove-social/" + j('#file_name').val(),
data: form_data,
headers: {
X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'DELETE', //POST TO DELETE
contentType: false,
processData: false,
success: function (data) {
j('#preview_image').attr('src', '{{URL::to('/public/assets/Webadmin/images/attach-1.png')}}');
j('#file_name').val('');
j('#loading').css('display', 'none');
},
error: function (xhr, status, error) {
alert(error);
alert(xhr.responseText);
}
});