简体   繁体   中英

How to redirect back with errors in Laravel 8 inside an Ajax request

When data.custom_input === "false" I want to redirect back with errors. redirect()->back() is giving me this error inside the console: Uncaught SyntaxError: Unexpected number: cd988e2f-ba9c-4b66-97d9-618e776ae740:157. Which is my uuid I pass to all routes.

URL::previous does work fine, but I can't pass any errors.

let interval = 1000;

function doAjax() {
$.ajax({
    type: 'POST',
    url: '/listen/custominput',
    data: {
        '_token': $('meta[name="csrf-token"]').attr('content')
    },
    dataType: 'json',

    success: function (data) {
        if (data.custom_input === "true") {
            window.location.href = "{{route('index', $link->id)}}";

        } else if (data.custom_input === "false") {
            window.location.href = {{redirect()->back()->withErrors(['error', 'has-error'])}};
        }
    },

    complete: function (data) {
        setTimeout(doAjax, interval);
    }
});
}
setTimeout(doAjax, interval);

What do I need to do?

而不是您的 (POST) 路由返回 custom_input = "false" 然后尝试在 JavaScript 中重定向回来,您应该在 (POST) 路由控制器(而不是刀片/js)中使用您已有的重定向:

redirect()->back()->withErrors(['error', 'has-error'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM