【问题标题】:How to set the url for XMLHttpRequest in JS?如何在 JS 中设置 XMLHttpRequest 的 url?
【发布时间】:2015-05-06 20:22:33
【问题描述】:

在 somefile.html 中,我使用了一个 JS,其中包含 XMLHttpRequest:

request.open('GET', 'static/name.json', true); 当我从这个地方调用 render_template('somefile.html') 时效果很好

@app.route('/') (由于请求在这里打开文件:http://localhost:5000/static/name.json

但是当我需要像这样从其他地方调用 render_template('somefile.html') 时:

@app.route('/conference//modifysession/', methods=['GET','POST']) 这是 Chrome 开发人员中显示的错误:

http://localhost:5000/conference/16/modifysession/static/name.json 404(未找到) 所以请求会自动将标头添加到我不想要的文件的 URL 中。

如何从 URL 中删除自动添加的部分?

【问题讨论】:

    标签: url


    【解决方案1】:

    您在 get 请求中使用的 url 是一个相对 url,如果您在 get 请求中使用 '/static/name.json'(注意前导 /),它将用于 http://localhost:5000/static/name.json

    【讨论】:

    • 非常感谢。我花了很多时间,它只是一个'/'! :-)
    猜你喜欢
    • 2016-07-08
    • 1970-01-01
    • 2011-01-20
    • 2011-12-24
    • 2017-05-13
    • 1970-01-01
    • 2015-06-07
    • 2022-10-18
    • 2020-04-07
    相关资源
    最近更新 更多