简体   繁体   中英

how to get response from servlet for ajax call

I had called a servlet using ajax.

        $.ajax({
        type: "post",
        url: "FileUploadServlet", //this is my servlet
        dataType: 'json',
        data:{"myJsonString":"myJsonString","aadhar":"aadhar"},
        //data: "myJsonString" ,
        beforeSend: function (request)
        {
            request.setRequestHeader("myJsonString", myJsonString);
            request.setRequestHeader("aadhar", aadhar);

        },
        success: function(response){
            // $('#uidrespon').html(response); 
            //alert(uidrespon);
        } 
        });

From my servlet i wanna pass the response to the ajax. For sending the response from the servlet I am using this using code but i am not getting response from through ajax

         protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json;charset=utf-8");
    PrintWriter out = response.getWriter();
    System.out.println("In doPost");

    String stat="";
    String member_Id="111111111";
    String session_id="121233";



    try{
        System.out.println("In try--------");
        String strrs = request.getHeader("myJsonString");
        String uid_val = request.getHeader("aadhar");
        System.out.println("Uid value..."+uid_val);
        if(strrs!= null){
             stat = "Uploaded Successfully";
             System.out.println("upload status"+stat);
        }else if(strrs == null){
            stat = "Uploaded Failed";
             System.out.println("upload status"+stat);
            request.setAttribute("status", stat);
        }   

        byte[] b = strrs.getBytes();
        System.out.println("In bytes---"+b);
        String encodedString = Base64.encodeBase64(b).toString();
        System.out.println("In image---\n"+encodedString);

         uid_webservice tes = new uid_webservice();

         String src= tes.authenticate(member_Id,uid_val,encodedString,session_id);

         System.out.println("Source------"+src); // Getting return value value from uid_webservice 

         request.setAttribute("uidrespon", src);

         response.setContentType("text/plain");  
         response.setCharacterEncoding("UTF-8"); 
         response.getWriter().write(src);

Can any one help me out...Thanx in advance

$.ajax({
    type: "post",
    url: "FileUploadServlet", //this is your servlet
    dataType: 'html',
    data:{
        myJsonString:"myJsonString",
        aadhar:"aadhar"
    },
    success: function(response){
         $('#uidrespon').html(response); 
        //alert(uidrespon);
    } 
    });

Servlet side:

request.setAttribute("src", src); 
response.setCharacterEncoding("UTF-8"); 
response.getWriter().print(src);

I think it must work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM