Javascript AJAX Post请求不能与Laravel一起使用

Javascript AJAX Post请求不能与Laravel一起使用,javascript,jquery,ajax,laravel,Javascript,Jquery,Ajax,Laravel,我可以这样做: $.ajax({ type: "GET", async: true, url: '/someurl/', dataType: 'json', success: function (data) { console.log(data); } }); 网站: 它工作得很好,但当我尝试使用post时: $.ajax({ type: "POS

我可以这样做:

 $.ajax({
        type: "GET",
        async: true,
        url: '/someurl/',
        dataType: 'json',
        success: function (data) {
            console.log(data);
        }
    });
网站:

它工作得很好,但当我尝试使用post时:

 $.ajax({
        type: "POST",
        async: true,
        url: '/someurl/',
        dataType: 'json',
        success: function (data) {
            console.log(data);
        }
    });

Route::post('/someurl','MyController@myfunction');

我在控制台中得到一条405方法不允许的错误消息,使用普通ajax需要CSRF令牌传入
POST方法

在您的ajax中

 $.ajax({
        type: "POST",
        async: true,
        url: '/someurl/',
        dataType: 'json',  
        data : {"_token":"{{ csrf_token() }}"}  //pass the CSRF_TOKEN()
        success: function (data) {
            console.log(data);
        }
    });

集合头元标记

<meta name="csrf_token" content="{{ csrf_token() }}" />

POST
使用普通ajax需要
CSRF令牌
传入
POST方法

在您的ajax中

 $.ajax({
        type: "POST",
        async: true,
        url: '/someurl/',
        dataType: 'json',  
        data : {"_token":"{{ csrf_token() }}"}  //pass the CSRF_TOKEN()
        success: function (data) {
            console.log(data);
        }
    });

集合头元标记

<meta name="csrf_token" content="{{ csrf_token() }}" />

添加另一条带有

Route::post('/someurl','MyController@myfunction');

顺便说一下,你没有发送任何数据,在post中,我们需要正确发送数据

还要检查csrf令牌是否正在数据中传递,如果不是如上所述,请尝试手动添加它


如果您使用的是
{{Form…}
它将自动添加到表单数据中。

添加另一条带有

Route::post('/someurl','MyController@myfunction');

顺便说一下,你没有发送任何数据,在post中,我们需要正确发送数据

还要检查csrf令牌是否正在数据中传递,如果不是如上所述,请尝试手动添加它


如果您使用的是
{{Form…}
它将自动添加到表单数据中。

Route::get(
Change
Route::get
to
Route::post
?然后去喝咖啡如果您将code
Route::get
更改为
Route::post
,不要忘了
CSRF
令牌,后面的斜杠是否重要?
Route::get(
Change
Route::get
to
Route::post
?然后去喝咖啡如果您将code
Route::get
更改为
Route::post
,不要忘了
CSRF
标记,后面的斜杠是否重要?