?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() }}"},
    
    评论

报告相同问题?