簡體   English   中英

如何在 Ajax 請求中重定向回 Laravel 8 中的錯誤

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

當 data.custom_input === "false" 我想重定向回錯誤。 redirect()->back() 在控制台中給我這個錯誤:Uncaught SyntaxError: Unexpected number: cd988e2f-ba9c-4b66-97d9-618e776ae740:157。 這是我傳遞給所有路線的 uuid。

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);

我需要做什么?

而不是您的 (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