use*_*025 -2 javascript ajax jquery laravel
我在Laravel项目中使用Ajax,我有一个按钮,可以触发一个有两个状态错误和成功的函数.但是在他们两个上都有成功的消息.
控制器:
if ($user->balance < $total_paid) {
return response()->json(['responseText' => 'error'], 201);
} else {
return response()->json(['responseText' => 'success!'], 200);
}
Run Code Online (Sandbox Code Playgroud)
前端的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>
Run Code Online (Sandbox Code Playgroud)
和按钮:
<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>
Run Code Online (Sandbox Code Playgroud)
我尽我所能,在互联网上做了很多研究,但没用.
| 归档时间: |
|
| 查看次数: |
529 次 |
| 最近记录: |