dongyuans61046 2018-01-19 07:16
浏览 308

Laravel ajax不会返回数据

I'm trying to create a post using ajax in Laravel. Ajax apparently works, but when calling the controller, it doesn't have any data.

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

Has an URL and works perfectly.

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

Returns false or 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.

$('[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?

  • 写回答

1条回答 默认 最新

  • dongyaobo9081 2018-01-19 08:10
    关注

    I found the answer, the text opcion in the ajax in my case need to be '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}
                })
            });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?