【问题标题】:Window Location Not Working as Expected窗口位置未按预期工作
【发布时间】:2013-02-17 18:20:20
【问题描述】:

我希望以下 JavaScript 函数重定向到与我的 routes 文件匹配的 URL。

function getUrl() 
{   
    var hash= $('#hashText').val();

    // When the user has not entered a hash, alert to inform them.
    if (!hash)
    {
        alert("Please enter a hash first.");
    }
    else
    {
        alert("Redirecting to ... http://localhost:8080/tasks/" + hash);
        window.location = "http://localhost:8080/tasks/" + hash;
    }
}

这是我的 routes 文件中对应的路由条目。

GET     /tasks/:hash                controllers.Application.getTask(hash: Int)

但是,这个函数的结果是:

http://localhost:9000/tasks?hash=2014281623

【问题讨论】:

    标签: javascript playframework routes


    【解决方案1】:

    如果你把你的代码改成这样怎么样:

    var url = 'http://localhost:9000/tasks/';
    url += escape(hash);
    window.location = url;
    

    【讨论】:

    • 感谢您的解释。但是,我怎样才能重定向到localhost:8080/tasks/<given ID>window.location = "localhost:9000/tasks/" + address 仍在将我重定向到 http://localhost:9000/tasks?hash=2014281623
    • 感谢您添加上述评论。如果您尝试使用 encodeURI(uri) 怎么样。
    • 即使使用encodeURI(uri),它仍然会将我带到http://localhost:9000/tasks?hash=2014281623
    • 您是否尝试过我在答案中提供的转义命令?如果这不起作用,那么服务器端一定有问题。
    • 看起来我需要这样做:window.location = "/" + hash; 考虑这个例子:jsfiddle.net/dzu6H/3
    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多