使用 javascript 重定向到相对于根路径的路径

Redirect to a path relative to root path using javascript

我正在做一个项目,我有一个表单来添加一个线程,如下所示:

共享按钮验证线程并将线程保存到数据库:

$('.share-thread').click(function() {
    // Validations

    let data = {
        '_token':csrf,
        'subject': $('#subject').val(),
        'category_id': $('#category').val(),
        'content':simplemde.value(),
        'thread_type': $('#thread_type').val()
    };

    $.ajax({
        type: 'post',
        data: data,
        url: '/thread',
        success: function(response) {
            window.location.href = "/" + response;
        }
    });
});

在控制器中,我 return 路径如下:/general/discussions 现在我想要的是如何在本地环境的情况下将此路径附加到根路径我想要这样的东西:

127.0.0.1/general/discussions

生产情况下:

www.domain.com/general/discussions

谢谢!

为什么不 return 使用 URL 助手之一的控制器的完整路径,例如 url() or route()

另外还有 Tighten 的 ziggy 包,它允许您在 JavaScript.

中使用命名路由

也许你可以相对重定向:

window.location.href = '../'; //According to the path levels

重定向到域路径

window.location.href = '/yourPath';  //Depending on the domain