Advertisement
TobNil

Untitled

Mar 17th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function(){
  2.  
  3.     var rootURL = "http://localhost:8080/api/students/";
  4.  
  5.     var currentStudent;
  6.  
  7.     $('#btnSave').click(function() {
  8.         addStudent();
  9.         return false;
  10.     });
  11.  
  12.     $('#btnTest').click(function() {
  13.         alert("asdasdas");
  14.         return false;
  15.     });
  16.  
  17.  
  18.     function addStudent() {
  19.         console.log('addStudent');
  20.         $.ajax({
  21.             type: 'POST',
  22.             contentType: 'application/json',
  23.             url: rootURL,
  24.             dataType: "json",
  25.             data: formToJSON(),
  26.             success: function(data, textStatus, jqXHR){
  27.                 alert('Student created successfully');
  28.                 //$('#studentId').val(data.id);
  29.             },
  30.             error: function(jqXHR, textStatus, errorThrown){
  31.                 alert('addStudent error: ' + textStatus);
  32.                 console.log("error: " + textStatus);
  33.             }
  34.         });
  35.     }
  36.  
  37.     function formToJSON() {
  38.         //var studentId = $('#studentId').val();
  39.         return JSON.stringify({
  40.             "firstName": $('#studentFirstName').val(),
  41.             "lastName": $('#studentLastName').val(),
  42.             "age": $('#studentAge').val()
  43.         });
  44.     }
  45.  
  46. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement