Advertisement
GWibisono

ajax-0.1a.js

Jun 1st, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ajaxGetShowId(url, targetId, async=true,debug=0){
  2.     var xhttp=xmlHttp();
  3.    
  4.     if(async!=true)async=false;
  5.     xhttp.open("GET", url, async);
  6.     xhttp.onreadystatechange = function () {
  7.         if(debug==1){
  8.             console.log(xhttp);
  9.         }
  10.         if (xhttp.readyState != 4 || xhttp.status != 200){
  11.             return 0;
  12.         }
  13.         else{
  14.             document.getElementById(targetId).innerHTML=xhttp.responseText;
  15.         }
  16.     };
  17.     xhttp.send();
  18. /*bila TIDAK menggunakan async maka bisa mengembalikan hasil*/
  19.     response={detail:xhttp, text:xhttp.responseText, state:xhttp.readyState, status:xhttp.status, url:xhttp.responseURL, type:xhttp.responseType}
  20.     return response;
  21. }
  22.  
  23. function ajaxGetJson(url,async=false, debug=0){
  24.     var xhttp=xmlHttp();
  25.    
  26.     if(async!=true)async=false;
  27.     xhttp.open("GET", url, async);
  28.     xhttp.onreadystatechange = function () {
  29.         if(debug==1){
  30.             console.log(xhttp);
  31.         }
  32.         if (xhttp.readyState != 4 || xhttp.status != 200){
  33.             return 0;
  34.         }
  35.     };
  36.     xhttp.send();
  37.     response=xhttp.responseText;
  38.     json=JSON.parse(response);
  39.     return json;
  40. }
  41.  
  42. function xmlHttp(){
  43.     if (window.XMLHttpRequest) {
  44.         xhttp = new XMLHttpRequest();
  45.         } else {
  46.         // code for IE6, IE5
  47.         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  48.     //new ActiveXObject("MSXML2.XMLHTTP.3.0")  
  49.     }
  50.     return xhttp;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement