Creating a json object in jsp and using it with JQuery

后端 未结 4 1549
既然无缘
既然无缘 2020-12-01 22:54

I\'ve created a JSP application, which gets results based on a user search (using lucene). I store the results in a Bean.

I\'m also using Jquery Ajax to display the

4条回答
  •  佛祖请我去吃肉
    2020-12-01 23:45

    Here's an example you may take a look at. Basically your JSP page might look like this:

    <%@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();
    %>
    

    and on the client:

    $.ajax({
        url : 'search.jsp',
        data : { search: 'test' },
        dataType: 'json',
        success : function(json) {
            alert(json.title);
        }
    });
    

    And here are even more examples.

提交回复
热议问题