I am new to jersey rest and ajax . I am trying to fetch data from form ,sending it as json data to my rest server using post method. The rest server returns back data.But i am getting internal server error.kindly help
<script src="http://ift.tt/1qRgvOJ"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="response">
<pre></pre>
</div>
<form id="my-form">
<input type="text" id="first-name" name="first-name" placeholder="First Name" />
<input type="text" id="last-name" name="last-name" placeholder="Last Name" />
<button type="submit">Submit</button>
</form>
<script>
(function($){
function processForm( e ){
$.ajax({
url: 'http://localhost:8080/jsonajax/rest/user/add',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify( { "first-name": $('#first-name').val(), "last-name": $('#last-name').val() } ),
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
e.preventDefault();
}
$('#my-form').submit( processForm );
})(jQuery);
</script>
</body>
</html>
package jsonajax;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
@Path("/user")
public class Hello {
@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addUser(
@FormParam("first-name") String first,
@FormParam("last-name") int last) {
return Response.status(200)
.entity("addUser is called, name : " + first + ", age : " + last)
.build();
}
}
Aucun commentaire:
Enregistrer un commentaire