xmlhttprequest icon indicating copy to clipboard operation
xmlhttprequest copied to clipboard

rewriting ajax requests

Open requiredfield opened this issue 11 years ago • 4 comments

I'm trying to use xmlhttprequest.js to intercept an ajax request and request a different url instead. The code all looks correct, but it's failing silently with no hints in the developer console. Here is a live preview along with the code:

http://embed.plnkr.co/5Vpdohf69anQlUutHl7Y/preview

Am I doing something wrong, or is this just not possible?

Thanks in advance for any tips.

requiredfield avatar Sep 08 '14 05:09 requiredfield

When I try your page, I see "XMLHttpRequest cannot load https://httpbin.org/bytes/8. Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers. " message in console. This may have something to do with configuration of access control on the server.

ilinsky avatar Sep 08 '14 06:09 ilinsky

Thanks so much for taking a look @ilinsky, and sorry I linked to a bad plunker! Fixed my example.

Here is a version where the conditional rewrite code is commented out and clicking the button successfully loads https://httpbin.org/ip:

http://embed.plnkr.co/5Vpdohf69anQlUutHl7Y/preview

When you uncomment out XmlHTTPRequest.js and the rewrite logic, after clicking the button the state stays stuck at "awaiting result", whether or not you check the rewrite checkbox:

http://embed.plnkr.co/dglwlMoR14nTHymvkssW/preview

This time it's rewriting a cross-origin request to a same-origin request, so it should be even more likely to work.

Looking in Developer Tools, there are no errors in the JavaScript console, and the XHR is never even opened or sent.

Do you have any idea what's going on here? Thanks again for any help!

requiredfield avatar Sep 08 '14 15:09 requiredfield

(you may have to Shift+Reload on http://embed.plnkr.co/5Vpdohf69anQlUutHl7Y/preview to make sure you get the newest version)

requiredfield avatar Sep 08 '14 15:09 requiredfield

Hi @ilinsky, did that example make sense / demonstrate the issue properly? Much appreciate any time you have to take a look.

requiredfield avatar Sep 11 '14 04:09 requiredfield