tags:

views:

139

answers:

1
$(document).ready(function() {
    $("form.ajax").submit(function(e) {
     destString = $(this).attr('action');
     dataString = $(this).serialize();

     $.ajax({
      type: "POST",
      url: destString,
      data: dataString,
      datatype: json,
      success: function(data) {
       if (!data.success)
       {
        $.amwnd({
         title: 'Error!',
         content: data.message,
         buttons: ['ok'],
         closer: 'ok'
        });
       }
      }
     });

     e.preventDefault();
     return false;
    });
});

That is some code I am using for making all forms I have with class="ajax" submit using Ajax. I have looked at some StackOverflow questions such as this one along with other sites on the web, which has led me put this block of code in:

e.preventDefault();
return false;

That's two things that apparently both stop the form submission via normal methods, but looks like that's not working.

The form I am testing with this is

<form action="add_comment.php" method="post" class="ajax">

Am I doing something wrong, or..?

+1  A: 
datatype: json

should be

dataType: 'json'

Note:
The above fixed the form from performing default submission behaviour, doesn't guarantee the ajax call will have a proper return as I have no idea what is being returned.

See Reference

o.k.w
Ah. Silly me. Thanks.
a2h
@a2h: Sorry I said in my comment in your post that your code 'should' work. I missed that until I tried the codes myself. Good luck!
o.k.w