Advertisement
gundambison

ajax0.3.js

Apr 27th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***
  2. ajax tanpa jquery
  3. untuk dicoba apakah lebih cepat? atau lebih lambat?
  4. ****/
  5. function ajaxGetShowId(url, targetId, async=true,debug=0){
  6.     var xhttp=xmlHttp();
  7.    
  8.     if(async!=true)async=false;
  9.     xhttp.open("GET", url, async);
  10.     xhttp.onreadystatechange = function () {
  11.         if(debug==1){
  12.             console.log(xhttp);
  13.         }
  14.         if (xhttp.readyState != 4 || xhttp.status != 200){
  15.             return 0;
  16.         }
  17.         else{
  18.             document.getElementById(targetId).innerHTML=xhttp.responseText;
  19.         }
  20.     };
  21.     xhttp.send();
  22. /*bila TIDAK menggunakan async maka bisa mengembalikan hasil*/
  23.     response={detail:xhttp, text:xhttp.responseText, state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  24.     return response;
  25. }
  26.  
  27. function ajaxGetJson(url,async=false, debug=0){
  28.     var xhttp=xmlHttp();
  29.    
  30.     if(async!=true)async=false;
  31.     xhttp.open("GET", url, async);
  32.     xhttp.onreadystatechange = function () {
  33.         if(debug==1){
  34.             console.log(xhttp);
  35.         }
  36.         if (xhttp.readyState != 4 || xhttp.status != 200){
  37.             return 0;
  38.         }
  39.     };
  40.     xhttp.send();
  41.     response={state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  42.     text=xhttp.responseText;
  43.     try {
  44.         json=JSON.parse(text);
  45.         response.result=json;
  46.         response.error=false;
  47.         response.message='OK';
  48.     }
  49.     catch(err) {
  50.         response.result=false;
  51.         response.error=err;
  52.         response.message=err.message;
  53.     }
  54.     return response;
  55. }
  56.  
  57. function ajaxPostJson(url,data=array(),async=false, debug=0){
  58.     var xhttp=xmlHttp();
  59.    
  60.     if(async!=true)async=false;
  61.     xhttp.open("POST", url, async);
  62. //  xhttp.setRequestHeader('Content-Type', 'application/json');
  63.     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  64.  
  65.     xhttp.onreadystatechange = function () {
  66.         if(debug==1){
  67.             console.log(xhttp);
  68.         }
  69.         if (xhttp.readyState != 4 || xhttp.status != 200){
  70.             return 0;
  71.         }
  72.     };
  73.     params= param(data); console.log(params);
  74.     xhttp.send( params );//JSON.stringify(data) );
  75.     response={state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  76.     text=xhttp.responseText;
  77.     try {
  78.         json=JSON.parse(text);
  79.         response.result=json;
  80.         response.error=false;
  81.         response.message='OK';
  82.     }
  83.     catch(err) {
  84.         response.result=false;
  85.         response.error=err;
  86.         response.message=err.message;
  87.     }
  88.     return response;
  89. }
  90.  
  91. function xmlHttp(){
  92.     if (window.XMLHttpRequest) {
  93.         xhttp = new XMLHttpRequest();
  94.         } else {
  95.         // code for IE6, IE5
  96.         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  97.     //new ActiveXObject("MSXML2.XMLHTTP.3.0")  
  98.     }
  99.     return xhttp;
  100. }
  101.  
  102. function param(object) {
  103.     var encodedString = '';
  104.     for (var prop in object) {
  105.         if (object.hasOwnProperty(prop)) {
  106.             if (encodedString.length > 0) {
  107.                 encodedString += '&';
  108.             }
  109.             encodedString += encodeURI(prop + '=' + object[prop]);
  110.         }
  111.     }
  112.     return encodedString;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement