I'm looking for a very small (one liner) Ajax JavaScript library to add on the first line of a small script to make some requests.
I already tried:
jx
Microajax
But they do not work at all. Alternatives?
With interactive websites and modern web standards, Ajax is gradually being replaced by functions within JavaScript frameworks and the official Fetch API Standard.
Yes, AJAX (XHR) is used all the time in web pages. It is still the primary way that JavaScript in a web page makes an in-page request to a server.
AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)
Here you go, pretty simple:
function createXHR() { var xhr; if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { alert(e.message); xhr = null; } } else { xhr = new XMLHttpRequest(); } return xhr; }
Documentation is here
Example:
var xhr = createXHR(); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { alert(xhr.responseText); } } xhr.open('GET', 'test.txt', true) xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.send()
Update:
In order to do cross-domain scripting, you'll either have to call out to a local server-side proxy (which reads and echo's the remote data), or, if your remote service returns JSON, use this method:
var s = document.createElement('script') s.src = 'remotewebservice.json'; document.body.appendChild(s);
Since JSON is essentially a JavaScript object or array, this is a valid source. You theoretically should then be able to call the remote service directly. I haven't tested this, but it seems to be an accepted practice:
Reference: Calling Cross Domain Web Services in AJAX
You can build your own version of jQuery that only includes the AJAX modules.
https://github.com/jquery/jquery#how-to-build-your-own-jquery
https://github.com/jquery/jquery#modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With