Reputation: 193
i need to call a servlet from javascript by using ajax post method and also need to send some parameters to that servlet. How can i do that. I have spent too much time to get rid of this but still no luck...! All help appreciated.....Please help
Upvotes: 1
Views: 9912
Reputation: 14
Try this
$.ajax({
type : 'GET',
url : '${pageContext.request.contextPath}/Servletname',
data : $('#formname').serialize(),
success : function(response) {
alert(response)
}
});
Upvotes: 0
Reputation: 66
using jQuery is very easy...
$.post("yourServletUrl",{"param1":"1","param2":"2"},function(data){},"json");
this is method post
first param: servlet url
second param: params
third param: callback
the last param: response data
format ("json","xml"...)
Upvotes: 5
Reputation: 46
Try this ...
jQuery.ajax({
url:URL,
data:{"key1":"value1","key2":"value2"},
type:'POST',
dataType : 'xml', /* use 'jsonp' for cross domain*/
success:function(data, textStatus, jqXHR){
// access response data
},
error:function(data, textStatus, jqXHR){
console.log('Service call failed!');
}
});
Upvotes: 2