Advertisement
kura2yamato

js ajax

Oct 29th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(".btnSubmit").click(function(){
  2.    form_id = $(this).parent().parent().parent();
  3.    url_target = form_id.attr('action');
  4.    var params =  form_id.serialize();
  5. //   console.log(form_id, params);
  6.    var respon = jQuery.ajax({
  7.           url: url_target,
  8.           type: "POST",
  9.           data: params,
  10.           dataType: "json", //disarankan return sebagai JSON.. json_encode($params);
  11.           cache:false,
  12.           timeout:20000
  13.         });
  14.        
  15.         respon.success(function(result, status) {
  16.             //console.log("OK",result,status);
  17.             //================LAKUKAN PROSES KALAU BENAR.. check nilai result.status
  18.             if (typeof result.status !== 'undefined') {
  19.                 if (result.status) {
  20.                     //==proses kl OK
  21.                     console.log('OK');
  22.                 } else {
  23.                     //==proses kl GAGAL
  24.                     console.log('gagal');
  25.                 }
  26.             } else {
  27.                 alert('not valid json');
  28.             }
  29.         });
  30.  
  31.         respon.error(function (xhr, status, msg) {
  32.             //console.log("ERROR",xhr,status,msg);
  33.             //alert(msg);
  34.             //================LAKUKAN PROSES KALAU SALAH
  35.         });
  36.  
  37.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement