php - 如何在 Laravel 6 中使用 Ajax 删除没有表单的项目
问题描述
我想在 Laravel 6 中使用使用 Ajax 的表单删除一个项目在我的路线中我有
Route::group(['middleware' => 'auth'], function() {
Route::get('/dashboard/desfazer-importe', 'DeleteImportController@index')->middleware('auth');
Route::delete('/dashboard/desfazer-importe/{id}', 'DeleteImportController@destroy')->middleware('auth');
});
我的看法
div class="modal-body">
<h5>Deseja realmente desfazer esta importação?</h5>
<input type="hidden" value="<?php $portal_id ?>" id="portal_id">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
<button type="button" class="btn btn-primary" id="delete" data-token="">Desfazer</button>
</div>
我通过隐藏的输入获取 id
在 Ajax 中我有以下代码
function deleteImport()
{
$('#delete').click(function() {
let porta_id = $('#portal_id').val()
$(this).prop("disabled",true);
$.ajax({
type: 'DELETE',
url: '/dashboard/desfazer-importe',
data: {
"id": porta_id,
},
success: function(response) {
console.log(response)
},
error: function(error) {
console.log(error)
}
})
})
}
它可能在不使用表单操作的情况下删除项目?我发送时收到此消息
message: "CSRF token mismatch."
解决方案
您必须将 csrf 令牌添加到您的请求中:https ://laravel.com/docs/7.x/csrf#csrf-x-csrf-token
看起来你还有另一个问题。您必须将您的项目 ID 添加到 url。/dashboard/desfazer-importe/{id}
推荐阅读
- python - Matplotlib Y 比例修改
- java - 具有通用和更改字段的工具的设计模式
- android - 计算姿势检测 mlkits 中骨骼关键点之间的距离,然后存储在 Firebase 数据集中
- html - 在文本和图像下定位 css 边框
- python - 矩阵的抽象类
- javascript - javascrpt 更改元标记内容
- spring - Swagger + spring boot + jwt + 如何禁用特定 API 的授权按钮
- html - 如何将 div 颜色更改为备用颜色?
- ios - 在选项卡视图之一中切换 TabView 中的选项卡“重置”matchedGeometryEffect()
- c++ - 如何使用 struct 读取带逗号的文件?