简体   繁体   English

如何在 Ajax 请求中重定向回 Laravel 8 中的错误

[英]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.当 data.custom_input === "false" 我想重定向回错误。 redirect()->back() is giving me this error inside the console: Uncaught SyntaxError: Unexpected number: cd988e2f-ba9c-4b66-97d9-618e776ae740:157. redirect()->back() 在控制台中给我这个错误:Uncaught SyntaxError: Unexpected number: cd988e2f-ba9c-4b66-97d9-618e776ae740:157。 Which is my uuid I pass to all routes.这是我传递给所有路线的 uuid。

URL::previous does work fine, but I can't pass any errors. URL::previous 工作正常,但我不能传递任何错误。

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'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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