<!DOCTYPE html>
<html>
<head>
<title>Rewriting Ajax requests test</title>
<link data-require="bootstrap-css@*" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<!--
<script src="https://rawgit.com/ilinsky/xmlhttprequest/master/XMLHttpRequest.js"></script>
-->
</head>
<body class="container">
<h1>Rewriting Ajax requests test</h1>
<!--
<div>
<label>
<input id="rewrite" type="checkbox" />
enable rewriting
</label>
</div>
-->
<div>
<button>make Ajax request for "https://httpbin.org/ip"</button>
</div>
<div>
response url: <code id="responseUrl"></code>
</div>
<pre id="result">awaiting send...</pre>
<!--
<script src="rewrite.js"></script>
-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
$(function () {
$('button').click(function () {
$('#result').text('awaiting result...');
var xhr;
$.ajax({
url: 'https://httpbin.org/ip',
xhr: function () {
return xhr = $.ajaxSettings.xhr();
}
}).done(function(respData) {
$('#responseUrl').text(xhr.responseURL);
if (typeof respData === 'object' && window.JSON) {
respData = JSON.stringify(respData, null, 2);
}
$('#result').text(respData);
});
});
});
(function () {
function log(){if(window.console){console.log(Array.prototype.slice.call(arguments))}}
XMLHttpRequest.onopen = function (method, url, async) {
log('XMLHttpRequest.open called:', 'method:', method, 'url:', url, 'async:', async);
};
XMLHttpRequest.onsend = function (data) {
log('XMLHttpRequest.send called:', 'data:', data);
};
XMLHttpRequest.onabort = function () {
log('XMLHttpRequest.abort called');
};
var readyStates = {0: 'UNSENT', 1: 'OPENED', 2: 'HEADERS_RECEIVED', 3: 'LOADING', 4: 'DONE'};
XMLHttpRequest.onreadystatechange = function () {
log('XMLHttpRequest.readystatechange called:', 'readyState:', readyStates[this.readyState]);
};
XMLHttpRequest.prototype.__orig_open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
var rewrite = document.getElementById('rewrite');
if (rewrite.checked) {
var newUrl = 'ajax.txt?original_url=' + encodeURIComponent(url);
log('rewriting AJAX request:', url, '->', newUrl);
url = newUrl;
}
XMLHttpRequest.prototype.__orig_open.call(this, method, url, async, user, pass);
};
})();
this is the content of ajax.txt
Why doesn't the Ajax request get rewritten when the checkbox is checked?