?Briella 2019-07-23 14:14 采纳率: 0%
浏览 55

Ajax结果成功null

I'm trying to figure out why my successful ajax alert is returning null from my controller.

I'm simply serializing a form, submitting that to the controller and parsing it in order to get an array breakdown.

The ajax is successful because the alert is triggered but the data, which should be the resulting array, is actually null in the alert.

What am I doing wrong?

view.blade

<form id="codeGeneration">
<?php $i = 1; ?>
@foreach($getRuleAttributes as $attributes)
@if($attributes->title == 'New')
    <tr>
        <td><input type="checkbox" name="attribute[{{$i}}]['checked']"></td>
        <td><label>{{ $attributes->title }}</label></td>
        <input type="hidden" name="attribute[{{$i}}]['attributet_id']" value="{{ $attributes->attributet_id }}">
        <input type="hidden" name="attribute[{{$i}}]['attribute_data']" value="0">
        <input type="hidden" name="attribute[{{$i}}]['attribute_type']" value="promo_codes">
    </tr>
@else
    <tr>
        <td><input type="checkbox" name="attribute[{{$i}}]['checked']"></td>
        <td><label>{{ $attributes->title }}</label></td>
        <td><input type="text" name="attribute[{{$i}}]['attribute_data']"></td>
        <input type="hidden" name="attribute[{{$i}}]['attributet_id']" value="{{ $attributes->attributet_id }}">
        <input type="hidden" name="attribute[{{$i}}]['attribute_type']" value="promo_codes">
    </tr>
@endif
<?php $i++; ?>
@endforeach
</form>

//ajax
var form_data = $("#codeGeneration").serialize();
    $.ajax({
       type:'POST',
       url:'savePromoInfo',
       data:{form_data:form_data},
        _token: '{{ csrf_token() }}',
        success: function(data){
          alert(data);
        }
    });

controller

public function savePromoInfo()
{
    $form_data = $request->form_data;
    parse_str($form_data, $my_array_of_vars);
    $attr = $my_array_of_vars['attribute'];

    return json_encode($attr);
}
  • 写回答

1条回答 默认 最新

  • weixin_33725807 2019-07-23 14:21
    关注

    Is it really successful? Can you try as a first line to return 'Ok'; for example to ensure that that's passing.

    I notice also that you send the token out of the data in the ajax, but that should be part of the data object.

    data:{form_data:form_data, _token: "{{ csrf_token() }}"},
    
    评论

报告相同问题?

悬赏问题

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