ajax没有调用控制器函数laravel

时间:2017-08-31 14:22:33

标签: javascript php jquery ajax laravel-5

我在javascript函数中使用以下代码

 $.ajax({
    type: "DELETE",
    url: '/delprofile',
    success:alert("Record deleted."),
        error:  alert("Record not deleted.")
});

我的路线和功能如下:

public function delprofile (Request $request){  
   DB::table('education')->where('id','=',7)->delete();
   return true;
}

Route::post('/delprofile','ProfileController@delprofile');

查询未执行任何删除。

3 个答案:

答案 0 :(得分:4)

由于您的AJAX正在将请求方法设置为DELETE,因此您必须对路线执行相同操作。

Route::delete('/delprofile','ProfileController@delprofile');

答案 1 :(得分:1)

我认为主要问题来自你的JS代码,你错过了success & error中的anonymos函数:

$.ajax({
    type: "DELETE",
    url: '/delprofile',
    success: function(){ alert("Record deleted.") },
    error:  function(){ alert("Record not deleted.") },
});

或者您也可以使用done/fail代替:

$.ajax({
    type: "DELETE",
    url: '/delprofile',
}).done(function() {
    alert("Record deleted.");
}).fail(function() {
    alert("Record not deleted.");
});

希望这有帮助。

答案 2 :(得分:0)

在ajax代码中添加 CSRF令牌

data: {'_token': $('meta[name="csrf-token"]').attr('content')
            },

讨论here