Ajax JS

JS

function setAjax(){var a;try{a=new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{a=new ActiveXObject('Microsoft.XMLHTTP')}catch(E){a=!1}} if(!a&&typeof XMLHttpRequest!='undefined'){a=new XMLHttpRequest()} return a} function Ajax(meth,url,data,func){var r=setAjax();r.onreadystatechange=function(){if(r.readyState==4){if(r.status==200){func(r.responseText)}else{func('Error 200')}}}

r.open(meth,url,!0);r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');r.send(data)}

Example

// Method POST

var url = 'example.php';

var data = 'post1=val1&post2=val2';

Ajax('POST',url,data,function(e){

    console.log(e);

});


// Method GET

var url = 'example.php?num=1';

Ajax('POST',url,null,function(e){

    console.log(e);

});