提交数据; 形式与ajax

What are the advantages of submitting data to a server via forms?

My preference is for ajax. I'm considering changing my web app so that all data is submitted via ajax, but I'm afraid that forms are doing more for me than I realize. I would do this by pulling data out of a form and putting it in an ajax call. This, among other things, would allow me to control the request method (i.e. use PUT and DELETE).

I'm using Rails and Backbone.

Things I already know:

  • rails automatically checks the csrf-token attribute of a form for you

  • setting a form's accept-charset to utf-8 prevents people from using
    odd characters (I presume)

If you're using jQuery, you can do $(this).serialize() on a form object, and it will include all the fields that would ordinarily be caught by the browser.

http://api.jquery.com/serialize/

$( "form" ).on( "submit", function( event ) {
  event.preventDefault();
  console.log( $( this ).serialize() );
});

There are no 'advantages', it depends on your needs an the way you implement it.

If you are using Backbone and rails, a good approach is to use the rails-api gem. https://github.com/rails-api/rails-api