Advertisement
GWibisono

ajax 002

Jun 1st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.31 KB | None | 0 0
  1. <!-- perhatikan perbedaan oding ini hanya pada area headernya saja -->
  2. <link rel="stylesheet" type="text/css" href="css/basic.css">
  3. <h1>load automatis dibawah</h1>
  4. <p>saat halaman ini di klik. automatis dibawah ini akan ada tulisannya. Silakan view source dan lihat bahwa dibawah tidak ada tulisan! Versi ini memasukkan perintah dalam fungsi</p>
  5. <p id='contoh'>kosong disini</p>
  6. code ajax<pre>
  7. $.ajax({
  8.     url: "webservice",
  9.     type: "GET",
  10.     success: function(d) {
  11.         console.log(d);
  12.     }
  13. });
  14. </pre>
  15. read more : https://www.sitepoint.com/jquery-vs-raw-javascript-3-events-ajax/
  16. <script>
  17.  res=ajaxGetShowId('001-get.php','contoh' );
  18.  console.log(res);
  19. /*
  20. Nama sementara
  21. */
  22. function ajaxGetShowId(url, targetId, async=true,debug=0){
  23. //  var r = new XMLHttpRequest();
  24.     var xhttp;
  25.     if (window.XMLHttpRequest) {
  26.         xhttp = new XMLHttpRequest();
  27.         } else {
  28.         // code for IE6, IE5
  29.         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
  30.     }
  31.     if(async!=true)async=false;
  32.     xhttp.open("GET", url, async);
  33.     xhttp.onreadystatechange = function () {
  34.         if(debug==1){
  35.             console.log(xhttp);
  36.         }
  37.         if (xhttp.readyState != 4 || xhttp.status != 200){
  38.             return 0;
  39.         }
  40.         else{
  41.             document.getElementById(targetId).innerHTML=xhttp.responseText ;
  42.             //return xhttp.responseText;
  43.         }
  44.     };
  45.     xhttp.send();
  46.     return xhttp;
  47. }
  48. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement