AJAX发布未将数据发送到laravel控制器 [英] AJAX post not sending data to laravel controller

查看:93
本文介绍了AJAX发布未将数据发送到laravel控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个非常常见的主题,但是我找不到任何解决方案!,我正在开发的应用程序是在Laravel 5.0下运行的,我需要将jquery ajax 中的一些数据发送到laravel controller ,我已经遵循使用ajax发布数据的教程,我已经按照以下步骤进行了全局配置,所以当我使用以下命令将发布请求发送到url时,我有了一个带有 csrf 令牌的元数据阿贾克斯,它只是发送令牌!但我没有给它发送的任何数据!

Maybe this is a very common topic, but i can't find any solution!, the app i'm developing is under Laravel 5.0, i need to send some data from jquery ajax to a laravel controller, i've followed this tutorial to post data using ajax, i've followed the steps and made the global configuration, so that i have a meta with the csrf token, when i send the post request to a url using ajax, it just sends the token!! but nothing of the data i give it to send!

这是我的ajax函数(我正在使用虚拟数据对其进行测试):

Here's my ajax func (i'm using dummy data to test it):

        $.ajax( {
            url        : '/reservacion/paso-uno/enviar',
            method     : 'post',
            data       : { name: "John", location: "Boston" }
        } );

但是当我在发布函子中 dd(\ Request :: all()); 时,我仅获得令牌,如果我检查标题表格数据,我也将仅获得此令牌:

but when i dd(\Request::all()); in the post func i only get the token, also if i check the headers form data i only get this:

这是标题的完整图片:

Here's a complete image of the headers:

以下是带有csrf的meta标签:

Here's the meta tag with the csrf:

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

这是全局ajax设置:

And here's the global ajax setup:

$.ajaxSetup({
                headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
            });

大更新

不知道为什么,也不知道怎么回事,我想这只是一个缓存问题,但是具有上述配置,它会发送数据,但是只有当我有<input type='submit' >并且在jquery设置中单击事件时,这种情况才会发生,因为如果我将其设置为Submit事件,它将在浏览器路径中使用查询字符串重新加载页面.

Don't know why, neither how, i guess it was just a cache problem, but having the above configuration it sends data, but that happens only when i have a <input type='submit' > and in jquery setup the click event, because if i setup it for the submit event it reloads the page with a query string in the browser path.

现在的问题是控制器的功能无法实现...当我单击按钮时,什么也没发生,数据已发送但没有到达laravel控制器.

Now the problem is that the function of the controller is not reached... when i click on the button nothing happens, data is send but it dont reaches laravel controller.

推荐答案

我希望这会对您有所帮助.

i hope this will help you.

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

设置路线

Route::get('/your/url/goes/here',
        [
        'uses' => 'TestController@testFunction'
        ]);

设置控制器功能

在顶部use Input;

public function iddtest()
{
print_r(Input::all());
}

请求如下

$.ajax({
    data: {data1:'data1',data2:'data2'},
    url: '/your/url/goes/here',
    type: 'POST',
    beforeSend: function (request) {
        return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
    },
    success: function(response){
        console.log(response);
    }
})

这篇关于AJAX发布未将数据发送到laravel控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆