【问题标题】:Modify url of an XMLHttpRequest object修改 XMLHttpRequest 对象的 url
【发布时间】:2010-08-08 09:18:11
【问题描述】:

作为this 问题的修复,我想知道是否有人知道在发送之前修改 XMLHttpRequest 对象的 URL 的方法。

理想情况下,我想在 beforeSend 事件(使用 jQuery $.ajaxSetup)中更改 uri(对其进行编码),而不是在我使用 $.ajax 的每个位置更改它

谢谢!

【问题讨论】:

    标签: jquery ajax xmlhttprequest


    【解决方案1】:

    根据the documentationbeforeSend()方法将XMLHttpRequest对象作为参数传递,其this指针设置为Ajax请求选项。 XMLHttpRequest 没有 URL 属性,但 W3C documentation 似乎表示对象的 open() 方法可以在给定实例上多次调用:

    beforeSend: function(xhr) {
        // if you're doing authenticated requests, you might have to
        // call the 5-argument form of open() instead
        xhr.open(this.type, this.url.replace( /* whatever with whatever... */ ), this.async);
      }
    

    这样做的一个潜在问题是调用open() 会清除所有已设置的请求标头,因此您可能需要在其中添加一些额外的代码来重新添加jQuery 在调用@987654331 之前设置的标头@。

    编辑: 果然。现在我已经查看了jQuery source,您是对的:URL 是在调用beforeSend() 方法之前设置的。希望以上修订对您有用。

    【讨论】:

    • 这是我首先尝试的,但是此时 XMLHttpRequest 对象已经构建好了,所以在请求选项中编辑 url 不起作用。我需要编辑传入的“xhr”实例。
    猜你喜欢
    • 1970-01-01
    • 2017-08-17
    • 2013-06-02
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多