开发者

Finding the URL of an XMLHttpRequest

开发者 https://www.devze.com 2023-01-03 17:10 出处:网络
I\'ve got some code that does an ajax request using jQuery, and ha开发者_JAVA技巧ndles success and error conditions. On an error, I want to find out what the URL I called was, so I can log it. This in

I've got some code that does an ajax request using jQuery, and ha开发者_JAVA技巧ndles success and error conditions. On an error, I want to find out what the URL I called was, so I can log it. This information appears to be contained in the XMLHttpRequest.channel, but firefox is complaining about accessing this -

Permission denied for <http://localhost:8081> to get property XMLHttpRequest.channel

Any ideas how I can determine the URL associated with an XMLHttpRequest? What's the security issue getting hold of this information? Cheers,

Colin


Ok - sorry about this - an answer is here

http://api.jquery.com/ajaxError/

specifically this code from above link -

$('.log').ajaxError(function(e, xhr, settings, exception) {
  if (settings.url == 'ajax/missing.html') {
    $(this).text('Triggered ajaxError handler.');
  }
});

shows how to access the request url in the event of an ajax error. Doesn't explain why the XMLHttpRequest.channel object is a no go though. Anyway, hopefully that will help others with a similar problem.


Well, you just add it ;]

XMLHttpRequest.prototype.baseOpen = XMLHttpRequest.prototype.open; 
XMLHttpRequest.prototype.open = function(method, url, async) { this._url = url; return XMLHttpRequest.prototype.baseOpen.apply(this, arguments); }; 

then you can later ask for xhr._url in your error handler.

PS: Sorry, just discovered this thread is old.


The security issue is cross domain XHR requests.

In FF2 you used to be able to override this in about:config, also see this blog and especially this preference:

user_pref("capability.policy.default.XMLHttpRequest.channel", "allAccess");

But that's all not possible anymore in FF3. And with a good reason.

Note that XMLHttpRequest.channel is Gecko-specific, so this wouldn't have worked in non-Gecko browsers.


Firebug presents this error with a trace that shows the URI used.

0

精彩评论

暂无评论...
验证码 换一张
取 消