2

I want to send parameters using jquery ajax. I am able to call servlet using jquery ajax but unable to send any parameters.. My code is---

function callServlet(){
            var abc='hello';                
            $.ajax({
                type: "POST",
                url: "../d3data",                
                dataType: "json",
                data: {name : abc},
                success:function(data){
                    if(data){
                        alert("worked");
                    }
                },
                error:function(){
                    alert('not worked.');
                } 

            })       
        };

My servlet's name is d3data.. In servlet I read this value using

String name=request.getParameter("name");

Please any one help me.....

3
  • Do you mean to use GET instead of POST? Commented Apr 13, 2013 at 7:26
  • 3
    Your URL is pointing to a d3data folder on the client's machine. Use http://server_ip_or_domain_name/whatever/d3data/ Commented Apr 13, 2013 at 7:27
  • no, that URL is relative to wherever the page was downloaded from. Commented Apr 13, 2013 at 7:57

3 Answers 3

1
function callServlet(){
            var abc='hello';                
            $.ajax({
                type: "GET",
                url: "../d3data",                
                dataType: "json",
                data: {"name" : abc},
                success:function(data){
                    if(data){
                        alert("worked");
                    }
                },
                error:function(){
                    alert('not worked.');
                } 

            })       
        };
Sign up to request clarification or add additional context in comments.

Comments

0

you need to use

$.ajax({
     type: "GET",

or request post parameters in controller

Comments

0

To retrieve data from a servlet, you should be using the GET method. Furthermore, GET requests should generally be "idempotent", such that repeating the same request over and over does not generate different data.

The POST method is intended for sending bulk data to a servlet, and in particular when the sending of that data is supposed to have side effects on the server, such that the request is not idempotent.

Comments