Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Small Ajax JavaScript library [closed]

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?

like image 396
Tommy B. Avatar asked Aug 12 '10 18:08

Tommy B.


People also ask

What is the replacement of Ajax?

With interactive websites and modern web standards, Ajax is gradually being replaced by functions within JavaScript frameworks and the official Fetch API Standard.

Is Ajax JavaScript still used?

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.

Is Ajax a library of JavaScript?

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)


2 Answers

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

like image 86
Ryan Kinal Avatar answered Oct 16 '22 23:10

Ryan Kinal


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

like image 23
msaspence Avatar answered Oct 17 '22 01:10

msaspence