Skip to content
Niko edited this page Oct 8, 2013 · 1 revision

In order to be useful, the jsForm often needs to transmit/receive json data. This is normally done via ajax.

jQuery brings its ajax functionality with it, so this is also the best to use:

submit a form to the server

$("#myform").submit(function(ev){
  ev.preventDefault(); // prevent normal submit
  // get the object
  var obj = $(this).jsForm("get");
  if(!obj) { // no object means some fields are invalid!
    alert("Please fill out the form correctly!");
    return;
  }

  // submit data to server
  $.ajax({
    url:"/action/saveJson",
    cache:false, // do not cache
    type:'POST',  // transmit as post
    contentType: 'text/json; charset=UTF-8', // make sure to use utf-8 encoding
    data:JSON.stringify(obj), // convert the object to json
    success:function(data){
      alert("saved");
    },
    error: function(data, textStatus) {
      alert("error");
    }
  });

});
Clone this wiki locally