I have a problem when trying to read the data that I sent to the server from client with ajax function.
To start, here is my ajax function;
$(document).ready(function(){
$('#btnReg').click(function(){
var email = document.getElementById('email').value;
var korIme = document.getElementById('korIme').value;
var lozinka = document.getElementById('pass1').value;
$.ajax({
type: 'POST',
url: 'http://localhost:8080/Web_Projekat/rest/korisnici/registracija',
contentType: 'application/json',
dataType: 'text',
data: formToJSON(korIme, lozinka, "Petar", "Petrovic", "Menadzer", "06315487", email, "slika"),
success: function(){
alert("Success");
},
error: function(){
alert("Error");
}
});
} else {
return false;
}
});
});
function formToJSON(korIme, lozinka, ime, prezime, uloga, telefon, email, slika) {
return JSON.stringify({
"korIme" : korIme,
"lozinka": lozinka,
"ime": ime,
"prezime": prezime,
"uloga": uloga,
"telefon": telefon,
"email" : email,
"slika": slika
});
}
The data is sent successfully to the server. Here is my rest code:
@Path("/korisnici")
public class KorisnikServis {
@Context
HttpServletRequest request;
@Context
ServletContext ctx;
@POST
@Path("/registracija")
@Consumes({ MediaType.APPLICATION_JSON})
@Produces(MediaType.TEXT_PLAIN)
public String registracija(String data) {
return data;
}
}
Sorry for the names of the classes cause they are not in english.
Now my question is: How can I parse the data that's sent and work with it? Because the variable data is just a string with json format. How can I extract let's say the email address that't sent from the client?
UPDATE I have added some jar files, and added action="" to my form. But now I get this error : "HTTP Status 415 - Unsupported Media Type" I am using apache tomcat 6
Thank you.