Laravel 5.6 Ajax 发布数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51219813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:53:00  来源:igfitidea点击:

Laravel 5.6 Ajax post data

phpjqueryajaxlaravellaravel-5.6

提问by Furya

I tried to send data to a page with Ajax post without form but it doesn't work.

我尝试将数据发送到带有 Ajax post 的页面,但没有表单,但它不起作用。

I guess my problem come from crs but I won't be able to find how to make it work.

我想我的问题来自 crs,但我将无法找到如何使其工作。

In the header of my page, I have ;

在我的页面的标题中,我有 ;

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

Then, for this example, I have a datatable with reorder plugin. When reorder event is trigger, I use Ajax call (but my problem happen no matter what I do to send post Ajax except with form).

然后,对于这个例子,我有一个带有重新排序插件的数据表。当重新排序事件被触发时,我使用 Ajax 调用(但无论我做什么发送 Ajax 后,除了表单之外,我的问题都会发生)。

table{{$batiment->id}}.on( 'row-reorder', function ( e, diff, edit )      {
   $.ajaxSetup({
headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

    $.ajax({
        url: 'etages/form',
        type: 'POST',
        data: {item_id : 1},
        dataType: 'json',

        success: function( data ) {
            console.log(data);

        }       
    })
});

My route to form :

我的路线形成:

Route::post('/etages/form', ['as' => 'etages.form', 'uses' => 'Copro\EtageController@form']);

And my function form from my controller (this function work well with form post data) :

我的控制器中的函数表单(此函数适用于表单发布数据):

public function form(Request $request)
{

    $request->flash();

    return response()->json([
        'error' => 'false'
    ]);
}

But every time I tried post data without form, no matter what trigger I use (datatable, onclick...) I have this message :

但是每次我尝试在没有表格的情况下发布数据时,无论我使用什么触发器(数据表,onclick ...),我都会收到以下消息:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 无消息

On chrome console I can see my post data, but on laravel's error page, post data is empty. I don't know if it's normal.

在 chrome 控制台上,我可以看到我的帖子数据,但在 laravel 的错误页面上,帖子数据为空。不知道正常不

Someone can help me with that problem ? I can't always use get to send this kind of data.

有人可以帮我解决这个问题吗?我不能总是使用 get 来发送这种数据。

Thank for your help.

感谢您的帮助。

回答by Amin Fazlali

Method 1:

方法一:

var csrf = $('meta[name="csrf-token"]').attr('content');
$.ajax({
    url: '/etages/form',
    type: 'POST',
    data: {item_id : 1, '_token': csrf},
    dataType: 'json',

    success: function( data ) {
        console.log(data);

    }       
})

Method 2:

方法二:

$.ajaxSetup({
headers: {
    'X-XSRF-TOKEN': decodeURIComponent(/XSRF-Token=([^;]*)/ig.exec(document.cookie)[1])
}
});

$.ajax({
    url: '/etages/form',
    type: 'POST',
    data: {item_id : 1},
    dataType: 'json',

    success: function( data ) {
        console.log(data);

    }       
})

回答by Deepesh

your link 'etages/form' , have you defined it in a api route. if you have defined in a api route, then it should be like this '/api/etages/form'.

您的链接 'etages/form' ,您是否在 api 路由中定义了它。如果你在 api 路由中定义了,那么它应该是这样的“/api/etages/form”。