Laravel中的AJAX POST请求表示找不到url



我正试图通过ajax请求将数据发布到控制器。但它找不到路线,并在控制台中显示以下内容。

POST http://127.0.0.1:8000/addNotification_action 404 (Not Found)

这是我下面的ajax调用。

function editNotification(obj) {
// alert(obj.id);
var obj_id = obj.id;
var id = obj_id.split("_");
$.ajax({
url: "{{ url('addNotification_action') }}",
type: 'POST',
dataType: 'json',
data: {edit_notification_id: id[1]},
})
.done(function(result) {
console.log(result);
$('#title').val(result['title']);
$('#description_notification').val(result['details']);
$('#edit_flag_notification').val(result['notification_id']);
})
.fail(function() {
alert("error");
});
}

我只是想处理控制器中的请求。请帮忙。感谢

首先,您应该创建一个路由并为该路由命名。您必须将ajax url生成为";路由(‘路由名称’(";。

// Route (for Laravel 8)
Route::post('addNotification_action', [NotificationController::class, 'notify_method'])->name('notify');
// Ajax Settings
....
url: "{{ route('notify') }}",
....

Just在Ajax URL 中给出admin/addNotification_action

最新更新