AJAX返回空白页并打印代码

时间:2017-12-08 15:16:24

标签: php jquery ajax laravel

AJAX函数应该在同一页面的div checkout-wrap中返回HTML。相反,它会重定向到空白页面并打印数组

控制器中的代码:

return ['success'=>1, 'msg'=> trans('app.payment_received_msg'), 'response' => $this->payment_success_html()];

印刷什么:

{"success":1,"msg":"Success","response":" <div class=\"payment-received\">\n    <h1> <i class=\"fa fa-check-circle-o\"><\/i> Thank You<\/h1>\n    \n    <a href=\"http:\/\/localhost\" class=\"btn btn-filled\">Home<\/a>\n   <\/div>"}

我正在使用Laravel。

Ajax功能

$.ajax({
    url : '{{route('pg_submit')}}',
    type: "POST",
    data: form, 
success : function (data) {
    if (data.success == 1){
       $('.checkout-wrap').html(data.response);
       toastr.success(data.msg, '@lang('app.success')', toastr_options);
    }
},
error   : function ( jqXhr, json, errorThrown ) {
    var errors = jqXhr.responseJSON;
    var errorsHtml= '';
    $.each( errors, function( key, value ) {
        errorsHtml += '<li>' + value[0] + '</li>';
    });
    toastr.error( errorsHtml , "Error " + jqXhr.status +': '+ errorThrown);
}
});

路由pg_submit

Route::post('checkout/pg', ['as' => 'pg_submit','uses' => 'CampaignsController@pgRedirect']);

1 个答案:

答案 0 :(得分:0)

也许你有行动&amp;表单上的method属性,如

<form action="/checkout/pg" method="post">

您可以删除属性并将ID或类提供给您的

<form id="pg_submit">

您可以像这样添加或修改脚本

$('#pg_submit').submit(function (e) {
    e.preventDefault();
    $.ajax({
        url : '{{route('pg_submit')}}',
        type: "POST",
        data: form, 
        success : function (data) {
            if (data.success == 1){
               $('.checkout-wrap').html(data.response);
               toastr.success(data.msg, '@lang('app.success')', toastr_options);
            }
        },
        error   : function ( jqXhr, json, errorThrown ) {
            var errors = jqXhr.responseJSON;
            var errorsHtml= '';
            $.each( errors, function( key, value ) {
                errorsHtml += '<li>' + value[0] + '</li>';
            });
            toastr.error( errorsHtml , "Error " + jqXhr.status +': '+ errorThrown);
        }
    });
});