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);
}
查看全部
weixin_33738982
?Briella
2019/07/23 14:14
  • laravel
  • html
  • ajax
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信

1个回复