Supriya
Supriya

Reputation: 141

Problem with retrieving Json text through ajax

I am unable to get Json text through servlet response.The servlet code is working. My Ajax code is faulty. The code...

var json = eval('(' + xmlhttp.responseText +')');

...is not returning anything. Is there any jar required to do so? Below is my code:

//Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    List result = new ArrayList();
    result.add(new SearchResponse("001", "User Manual", "Operator"));
    response.setContentType("application/json");         
    response.setCharacterEncoding("UTF-8");        
    response.getWriter().write(new Gson().toJson(result));
}

In my Ajax, I am writing the following code to get it.

function ajaxFunction() {
    alert("function called...");
    if (xmlhttp) {
        alert(AJAX_SERVLET);
        xmlhttp.open("GET", AJAX_SERVLET, true); //AJAX_SERVLET has the servlet path
        xmlhttp.onreadystatechange = handleServerResponse;
        xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttp.send(null);
    }
}

function handleServerResponse() {
    alert(xmlhttp.readyState);
    if (xmlhttp.readyState == 4) {
        alert(xmlhttp.status);
        if (xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
            alert(json);
            var json = eval('(' + xmlhttp.responseText +')');
            request.setAttribute("output",json);
        } else {
            alert("Error during AJAX call. Please try again");
        }
    }
}

Upvotes: 0

Views: 887

Answers (2)

Shyam
Shyam

Reputation: 11

I Had same problem but i changed initilization part & in my case its working.

Blocking Code :

     var json = eval('(' + xmlhttp.responseText +')');   

Working Code:

var json = "";  
     json = eval('(' + xmlhttp.responseText +')');     

Regards Shyam Miyatra

Upvotes: 1

psema4
psema4

Reputation: 3047

I can't help with JSP, but hopefully this example will help for the JavaScript parts:

<script>
...
function handleServerResponse() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var json = JSON.parse(xmlhttp.responseText);

            // json now contains an array of objects, eg:
            //
            // [
            //   {
            //     "productNumber"  : "001",
            //     "productType"    : "User Manual",
            //     "funcDesignation": "Operator"
            //   }
            // ]

            // grab the first (and only) object in the array
            var firstRecord = json[0];

            // update the UI by selecting DOM elements and rewriting their contents
            document.getElementById('product-number').innerHTML =
                firstRecord.productNumber;

            document.getElementById('product-type').innerHTML =
                firstRecord.productType;

            document.getElementById('func-designation').innerHTML =
                firstRecord.funcDesignation;

        } else {
            alert("Error during AJAX call. Please try again");
        }
    }
}
</script>
...
<h4>Product</h4>
<div>Number: <span id="product-number"></span></div>
<div>Type: <span id="product-type"></span></div>
<div>Function: <span id="func-designation"></span></div>
...

PS - You might want to consider jQuery, MooTools or another modern JavaScript framework; they make AJAX calls and working with the DOM much easier.

Upvotes: 0

Related Questions