vips
vips

Reputation: 193

how to call a servlet using ajax (post) to send parameters...?

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

Answers (3)

jitesh
jitesh

Reputation: 14

Try this

                       $.ajax({

                        type : 'GET',

                          url : '${pageContext.request.contextPath}/Servletname',

                          data : $('#formname').serialize(),

                           success : function(response) {

                            alert(response)

                        }

                    });

Upvotes: 0

zhangqian
zhangqian

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

Jasim Mahamood
Jasim Mahamood

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

Related Questions