【问题标题】:How to execute fetch paths without interfering with webpage pathname?如何在不干扰网页路径名的情况下执行获取路径?
【发布时间】:2021-12-21 17:29:23
【问题描述】:

我正在做一个 Django 项目,但被 url 模式和路由卡住了。

index.html 上,路径名为"",JS 函数运行良好。获取 url tweets/${tweetid} 与 Django API 路由 url 模式 tweets/<int:tweetid> 匹配并且工作正常。

但是,在profile.html 上,路径名是"profile/<int:userid>",JS 在这里不起作用。获取 url tweets/${tweetid} 产生最终路径 profile/tweets/<int:tweetid>。因为这不在urls.py 中,所以我收到了一个错误。

如何在此 Web 应用程序的任何 html 页面上执行我的 fetch 和 JS 函数?

【问题讨论】:

    标签: javascript django fetch url-pattern


    【解决方案1】:

    您应该使用前导斜杠 (/) 开始路径以使用 绝对 路径,而不是 相对 路径,因此:

    <strong>/</strong>tweets/${tweetid}

    前导斜线表示它将获取页面hostname.ext/tweets/1234。如果您使用相对路径(没有前导斜杠),该项目将附加到已经指定的路径。

    【讨论】:

    • 谢谢!你救了我的命!
    猜你喜欢
    • 2021-01-25
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多