Anchy Anchy - 3 months ago 10
Ajax Question

My jquery form is not working in Firefox?

I have problem with making this form to work in Firefox but it works perfectly in Goole Chrome. I can see in network, after I press button, that is calling test.php and then I get error: object, object... I have no clue which part of Ajax function dose not work in Firefox and how to fix it. Hope someone can help me :)

$(document).on('submit','form#myform',function(){
$.ajax({
type:'POST',
url: '/wp-content/themes/html5blank-stable/test.php',
data: $('form').serialize(),
success: function(data) {
$( "#addEmilInfo").append( data);
},
error: function(error){
alert(error);
}
});
});


And I tried using event.preventDefault();, but then button would not react at all.

Answer

Do not submit form while calling ajax . Try below code :

$(document).on('submit','form#myform',function(e){
    e.preventDefault(); //prevent default submit action
  $.ajax({
           type:'POST',
           url: '/wp-content/themes/html5blank-stable/test.php',
           data: $('form').serialize(),
           success: function(data) {
              $( "#addEmilInfo").append( data);
        },
        error: function(error){
            alert(error);
        }
    });
});

e.preventDefault(); will prevent form submit action and will call your ajax url without submitting the form

Comments