1

Am getting a json data from servlet to JSP. Am getting the response from servlet checked in browser console. Attached the screenshot. I have given below the servlet and JSP code. Am making a ajax call to servlet to retrieve the json data. But from servlet response is coming but in ajax call its not going inside success function. How can i read this data in JSP and i need to use this JSON in javascript to retrieve it.

Am new to JSP & Servlet. I have tried this much. Can anyone help here please to read it and retrieve in JSP?

Thanking in Advance

Servlet Code

                    response.setContentType("application/json");

                    response.setHeader("cache-control", "no-cache");
                    PrintWriter out = response.getWriter();
                    response.setCharacterEncoding("UTF-8");
                    response.getWriter().write(json);

                    out.println(json.toString());
                    out.flush();

JSP Code with AJAX Call

            $.ajax({ 

            url: '<%=request.getContextPath()%>/home',
            type: 'POST',
            data: { 
                'action': 'getBugReport',
                'dateString':document.getElementById("daterangePicker").value,
                'projectName': 'All'
            },

           
            success: function(responseText) {
                },
           
            error: function () {
            }

Response from Servlet

Response from Servlet

  • what is output of `responseText` ..do `alert(responseText)` inside success function and see. Also you are returning data two times `response.getWriter().write(json);..` and `out.println(json.toString());..` ? – Swati Sep 16 '20 at 08:17
  • It is not going inside successfunction itself.. – Bhargava Hg Sep 16 '20 at 08:35
  • add `dataType: "json"` to your ajax .. Also , is there any other error showing in browser console ? and remove this line `out.println(json.toString());` as well. – Swati Sep 16 '20 at 08:42
  • I did above two changes. still no luck. :( no console error.. in console its showing the header response... with headerText am getting the required data. – Bhargava Hg Sep 16 '20 at 09:35
  • Try adding `error` function as well in ajax .i.e : `error: function(jqXhr, textStatus, errorMessage){console.log("Error: ", errorMessage); }` .Also , check [this](https://stackoverflow.com/questions/1969476/ajax-success-event-not-working) post. – Swati Sep 16 '20 at 10:21
  • Error: SyntaxError: Unexpected token { in JSON at position 9760 at parse () at ajaxConvert (jquery-1.12.4.js:8787) at done (jquery-1.12.4.js:9255) at XMLHttpRequest. (jquery-1.12.4.js:9548) – Bhargava Hg Sep 16 '20 at 12:50
  • Am getting this error after adding error method like above – Bhargava Hg Sep 16 '20 at 12:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221569/discussion-between-swati-and-bhargava-hg). – Swati Sep 16 '20 at 12:55
  • Looks like you write the json twice? `response.getWriter().write(json);` and `out.println(json.toString());`. Try to remove one of them, and try again. –  Sep 17 '20 at 15:07

0 Answers0