Analyzing JSON data from JSP

advertisements

i have a jsp on a server that returns me a JSON like this: {"link":"LINK_TEST","title":"TITLE_TEST"}

I am trying to get this data from a client html page but i can do it.

this is the jsp code:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
    JSONObject json = new JSONObject();
    json.put("title", "TITLE_TEST");
    json.put("link", "LINK_TEST");
    out.print(json);
    out.flush();
%>

The jsp looks like this: {"link":"LINK_TEST","title":"TITLE_TEST"}

And this is the client side code:

<!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script>
            $.ajax({
                url : 'http://my_ip:my_port/json.jsp',
                data : { search: 'test' },
                dataType: 'json',
                success : function(json) {

                var ob = jQuery.parseJSON(json);
                alert(ob.link+" "+ob.title);

                document.getElementById("uno").innerHTML = ob.link;
                document.getElementById("dos").innerHTML = ob.title;
                }

            });
    </script>
    </head>
    <body>

    <h1 id="uno"></h1>
    <h1 id="dos"></h1>

    </body>
    </html>


   Put it inside document.ready it will work and comment out datatype . i..e,
   <pre>
   &lt;script&gt;
    $(document ).ready(function() {
    $.ajax({
        url : 'http://localhost:8080/json.jsp',
        data : { search: 'test' },
        success : function(json) {
            var ob = jQuery.parseJSON(json);
            alert(ob.link+" "+ob.title);
            document.getElementById("uno").innerHTML = ob.link;
            document.getElementById("dos").innerHTML = ob.title;
        }
     });
   });
   &lt;/script&gt;
   </pre>