0

I am trying to send an json object array from a servlet to javascript .where I all get the array and parse . my ajax call the servlet appropriately but unable to recieve the json array at the javascript end please help

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    System.out.println("Post!!");
    response.setContentType("application/json");//since sendign jsonArray toString
    PrintWriter out = response.getWriter();
    try {
        Marker marker=new Marker( 40.72318,-74.03605);//

        JSONArray arrayObj=new JSONArray(); 
        arrayObj.add(marker);
        System.out.println(marker.toString());
        out.print(arrayObj);

    } finally {
        out.flush();
        out.close();
    }       

}

This is my ajax call in javascript where I am trying to get the json object array form the servlet.

  $.ajax({
          url:'test',
          dataType:'json',
          type:'POST',
          success:function(data){

            <%System.out.println(" success");%>
          console.log(data);
          alert('got json hopefully');
          alert(data);
          //
      },
      error:function(jxhr){
          <%System.out.println(" faliure");%>
        console.log(jxhr.responseText);
      }

}); 
1
  • What are you getting as response? Commented Dec 13, 2012 at 22:10

2 Answers 2

1

This worked for me, below is ajax code.

$.ajax({
    type : 'POST',
    url : "URL",
    data: {array: array},
    success : function(response)
    {
        alert("Success "+response.message);
    },
    error : function(response)
    {
        alert("Error"+response.message);
        console.log('Error');
    }
});

Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    String[] array = request.getParameterValues("array[]");
    request.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    ArrayList<String> message = new ArrayList<String>();
    message.add("response message goes here");
    PrintWriter writer = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("message",message); 
    response.setStatus(200);
    //System.out.println(obj.get("message"));
    writer.append(obj.toString());
    writer.close();
}

For more details refer here

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

Comments

0

This is how I make requests to the servlet and respond back with json. I use google GSON library for java and I advise you use it too. https://code.google.com/p/google-gson/

$.ajax({
                url: 'servletName', //the mapping of your servlet
                type: 'POST',
                dataType: 'json',
                data: $('#someForm').serialize(),
                success: function(data) {
                        if(data.isValid){
                     //maybe draw some html
                }else{
                    //data is not valid so give the user some kind of alert text
                }
                }

This is the servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String someInput = request.getParameter("someInput");

     Map map=new HashMap();


       if(someInput!=null){ //or whatever conditions you need
              map.put("isValid",true);
              map.put("someInput",someInput); 
       }else{
       map.put("isValid", false);

       }
       write(response,map);
   }



private void write(HttpServletResponse response, Map<String, Object> map) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(map)); //this is how simple GSON works
}

I hope this is clear. If not, ask me in the comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.