Menu Search Me

JQuery AJAX Functions

Below is an example of using JQuery's AJAX $post() method.

This function will post back profile data using ajax, and display result when response comes back asynchronously. The result is a JSON string:

function profileChange() {
   var result = "";
   var message = "";
   email = document.getElementById("email").value;
   firstname = document.getElementById("firstname").value;
   lastname = document.getElementById("lastname").value;
   var url = "/profilechange.do";
   $.post(url,
   {"email":email,
      "firstname":firstname,
      "lastname":lastname},
      function(data){
         result = JSON.parse($.trim(data)).result;
         if (result=="error") {
            message = "There is an error!";
            $("#txtMessage").text(message);
            $("#txtMessage").css("color", "red");
        }
        else {
           message = "Your change is successfully saved!";
           $("#txtMessage").text(message);
           $("#txtMessage").css("color", "black");
       }
    });
}