mercredi 19 juin 2019

event.preventDefault(); not working in ajax post lravel Framework 5.8.17

Hello generally it works elsewhere but this specific AJAX Post preventDefault() not working for me.

I am getting some value dynamically, I am setting that value to form's hidden fields and then triggering ajax Post to another ROUTE.

Following is code part for which i need any help or assistance.

<!--Init_Form_ST-->
<div style="display:none;">
  <form id="frm_init" name="frm_init" action="" method="POST"   >
    
    
    <input type="hidden" name="init_messageType" id="init_messageType" value="chatMessage" >
    <input type="hidden" name="init_idempotencyid" id="init_idempotencyid" value="" >
    <input type="hidden" name="init_receveruserid" id="init_receveruserid" value="" >
    <input type="hidden" name="init_lang" id="init_lang" value="" >    
    <?php /*?><input type="submit" name="init_submit" id="init_submit" value="init_sub" ><?php */?>
    <button type="submit" name="init_submit" id="init_submit" ></button>
  </form>        
</div>
<!--Init_Form_EN-->

I tried different methods to prevent form from getting submitted and navigate to another page.

alert("1st Alert - Before Click Trigger");
$("#init_submit").trigger('click');
alert("2nd Alert - After Click Trigger");

$("#frm_init").on('submit', function(event) {
  event.preventDefault();

  alert("3rd Alert - Into submit"); // <== It never comes here

  var init_formdata = new FormData(this);
  var init_msg_route = "";

  $.ajax({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    url: init_msg_route,
    dataType: 'json',
    cache: false,
    async: false,
    type: "POST",
    data: init_formdata,
    contentType: false,
    processData: false,
    success: function(result) {
      alert("Success");
    },
    error: function(jqxhr, status, exception) {
      alert('Exception:', exception);
    }
  });
});

pointer Goes to 1st Alert and waits for button click. After that pointer shows 2nd Alert but without waiting page navigates to route path.

I even tested by removing all Ajax code. But 3rd Alert never triggers

I have already tested with following options

// $("#frm_init").submit(function(event){
// $(document).on("submit","frm_init", function(event) {                                                                
// $(document).on("click","#init_submit", function(event) { 
// $("#init_submit").click(function(event){



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2MVHwlX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire