Ajax调用触发成功事件但返回201错误

时间:2017-07-06 11:20:01

标签: javascript jquery ajax laravel

我在Laravel项目上使用Ajax,我有一个按钮,可以触发一个有两个状态错误和成功的函数。但是在他们两个上都有成功的消息。

控制器:

if ($user->balance < $total_paid) {
return response()->json(['responseText' => 'error'], 201);
} else {
return response()->json(['responseText' => 'success!'], 200);
}

前端的Ajax函数:

<script type="text/javascript">
            function pay_with_ewallet()    { 
            $.ajax({
              url: "{!!URL::route('pay_with_ewallet')!!}",
              type: "post",
              data: {
                'OrderShipAddress':$('input[name=OrderShipAddress]').val(), 
                '_token': $('input[name=_token]').val(),
                'OrderEmail':$('input[name=OrderEmail]').val(),
                'city_id':$('select[name=city_id] :selected').val(),
                'OrderPhone':$('input[name=OrderPhone]').val(),
                'shipping_cost': @if (isset( $shipping_total )) {{ $shipping_total  }} @endif ,
                'order_total':@if (isset( $final_total )) {{ $final_total  }} @endif,
                },
                success: function(data) {
                    if (response == 'success') 
                        $("#myElem").show();
                        setTimeout(function() { $("#myElem").hide(); }, 10000);
                    else 
                        $("#myerror").show();
                        setTimeout(function() { $("#myerror").hide(); }, 10000);
                }​
            });
        }
    </script>

和按钮:

<button href="" class="btn btn-danger pull-right" id="pay_with_ewallet-btn {{ $user->id }}" data-id="{{ $user->id }}" onclick="pay_with_ewallet('pay_with_ewallet{{ $user->id }}')">{{ trans('interface.pay_with_e_wallet') }}</button>

我尽我所能,在互联网上进行了大量研究,但没用。

1 个答案:

答案 0 :(得分:5)

201是现有的HTTP CODE响应,表示created和已完成的请求。所以对于Ajax来说是成功的。尝试使用不同的数字,超出标准(100-505)。

https://httpstatuses.com/201

相关问题