简体   繁体   English

Laravel Ajax不返回数据

[英]Laravel ajax doesn't return data

I'm trying to create a post using ajax in Laravel. 我正在尝试在Laravel中使用Ajax创建帖子。 Ajax apparently works, but when calling the controller, it doesn't have any data. Ajax显然可以工作,但是在调用控制器时,它没有任何数据。

url: $(this).data("route"),

Has an URL and works perfectly. 有一个网址,可以完美运行。

var opciones = $("[name=cargaYDescarga]").bootstrapSwitch('state');

Returns false or true. 返回false或true。 I think there's not a problem with this data type. 我认为此数据类型没有问题。 I need to return this data to the controller and do a SQL or other. 我需要将此数据返回到控制器并执行SQL或其他操作。

$('[data-click="state"]').on('switchChange.bootstrapSwitch', function () {
        var opciones = $("[name=cargaYDescarga]").bootstrapSwitch('state');
        console.log(opciones);
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            url: $(this).data("route"),
            type: 'POST',
            data: {opcion: opciones}
        })
    });

    //Controller

    public function opcion(Request $request)
      {
         dd($request->input('opcion'));
      }

So, the question is: Do I have an error in this code? 因此,问题是:该代码中是否存在错误? Ajax can´t pass true/false directly and I need to convert into a string? Ajax无法直接传递true / false,我需要转换成字符串吗?

I found the answer, the text opcion in the ajax in my case need to be 'opcion'. 我找到了答案,在我的案例中,ajax中的文本opcion必须是“ opcion”。 See the code. 参见代码。

$('[data-click="state"]').on('switchChange.bootstrapSwitch', function () {
            var opciones = $("[name=cargaYDescarga]").bootstrapSwitch('state');
            console.log(opciones);
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url: $(this).data("route"),
                type: 'POST',
                data: {'opcion': opciones}
            })
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM