Change the url of the XMLHttpRequest object

As a fix for this problem, I was wondering if anyone knew about changing the URL of the XMLHttpRequest object before it was sent.

Ideally I want to change the uri (encode it) in the event beforeSend

(using jQuery $. AjaxSetup ) instead of changing it in all places where I use $ .ajax

Thank!

+2


source to share


1 answer


According to the documentation , the method beforeSend()

is passed XMLHttpRequest

as a parameter and the pointer is this

set to the Ajax request parameters. XMLHttpRequest

has no attribute, which is a url, but the W3C documentation seems to say that an object method open()

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 problem with this is that the call open()

clears any request headers that have been set, so you may need to add additional code to re-add the headers that jQuery sets before the call beforeSend()

.

EDIT: Of course. Now that I looked at the jQuery source , you are correct: the url is set prior to the method beforeSend()

call.Hopefully the above changes will work for you.

+4


source







All Articles