Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an AJAX call without jQuery?

How can I make an AJAX call using JavaScript, without using jQuery?

like image 769
discky Avatar asked Dec 19 '11 20:12

discky


People also ask

Can Ajax be used without jQuery?

Using Ajax requires jQuery, whilst it is a convenience, including the whole jQuery library for just Ajax is overboard. Here is a function that replaces Ajax (plus jQuery) and allows you to do GET, PUT, POST and DELETE HTTP calls. This is all done with XMLHttpRequest (XHR).

Can Ajax work without JavaScript?

AJAX isn't possible without Javascript, because it presupposes JS code running on the client. If JS is disabled, there's nothing that can execute in the browser and contact the server - only "dead" HTML and CSS. Flash is an alternative, but then again it can be disabled too.

Can we use JavaScript without jQuery?

Don't get me wrong - jQuery is still a wonderful library and most often than not you will be better off using it. However, for smaller things like simple pages with limited JS interactions, browser extensions and mobile sites, you can use vanilla JS.


2 Answers

With "vanilla" JavaScript:

<script type="text/javascript"> function loadXMLDoc() {     var xmlhttp = new XMLHttpRequest();      xmlhttp.onreadystatechange = function() {         if (xmlhttp.readyState == XMLHttpRequest.DONE) {   // XMLHttpRequest.DONE == 4            if (xmlhttp.status == 200) {                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;            }            else if (xmlhttp.status == 400) {               alert('There was an error 400');            }            else {                alert('something else other than 200 was returned');            }         }     };      xmlhttp.open("GET", "ajax_info.txt", true);     xmlhttp.send(); } </script> 

With jQuery:

$.ajax({     url: "test.html",     context: document.body,     success: function(){       $(this).addClass("done");     } }); 
like image 190
dov.amir Avatar answered Sep 21 '22 13:09

dov.amir


Using the following snippet you can do similar things pretty easily, like this:

ajax.get('/test.php', {foo: 'bar'}, function() {}); 

Here is the snippet:

var ajax = {}; ajax.x = function () {     if (typeof XMLHttpRequest !== 'undefined') {         return new XMLHttpRequest();     }     var versions = [         "MSXML2.XmlHttp.6.0",         "MSXML2.XmlHttp.5.0",         "MSXML2.XmlHttp.4.0",         "MSXML2.XmlHttp.3.0",         "MSXML2.XmlHttp.2.0",         "Microsoft.XmlHttp"     ];      var xhr;     for (var i = 0; i < versions.length; i++) {         try {             xhr = new ActiveXObject(versions[i]);             break;         } catch (e) {         }     }     return xhr; };  ajax.send = function (url, callback, method, data, async) {     if (async === undefined) {         async = true;     }     var x = ajax.x();     x.open(method, url, async);     x.onreadystatechange = function () {         if (x.readyState == 4) {             callback(x.responseText)         }     };     if (method == 'POST') {         x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');     }     x.send(data) };  ajax.get = function (url, data, callback, async) {     var query = [];     for (var key in data) {         query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));     }     ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async) };  ajax.post = function (url, data, callback, async) {     var query = [];     for (var key in data) {         query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));     }     ajax.send(url, callback, 'POST', query.join('&'), async) }; 
like image 38
Petah Avatar answered Sep 19 '22 13:09

Petah