As a fix for this issue, I was wondering if anyone knows of a way to modify the URL of an XMLHttpRequest object prior to sending it.
Ideally, I want to chan开发者_JS百科ge the uri (encode it) in the beforeSend
event (using jQuery $.ajaxSetup) instead of changing it in every location where I'm using $.ajax
Thanks!
According to the documentation, the beforeSend()
method is passed the XMLHttpRequest
object as a parameter, and its this
pointer is set to the Ajax request options. The XMLHttpRequest
doesn't have an attribute that is the URL, but the W3C documentation seems to say that the object's open()
method can be called multiple times on a given instance:
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);
}
One potential issue with doing this is that calling open()
clears all of the request headers that have been set, so you may have to add some additional code in there to re-add the headers that jQuery sets before invoking beforeSend()
.
EDIT: Sure enough. Now that I've taken a look at the jQuery source, you're right: the URL is set before the beforeSend()
method is invoked. Hopefully the revisions above will work for you.
精彩评论