-3

This is my ajax request

var id = $(this).attr('data-id');

$.ajax({
    type: 'POST',
    url: '../include/residents.jsp',
    dataType: "json",
    data: {
        id: id,
    },
    success: function(data) {},
    error: function(data) {}
}).done(function() {
});

This is that is inside residents.jsp

<%@ page import="java.io.*,java.util.*, javax.servlet.*,java.text.*" %>
<%@ page import="javax.swing.*" %>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@ include file="dbconfig.jsp" %>

<% 
    String id = request.getParameter("id");


    try {

        conn.setAutoCommit(false);
        PreparedStatement preparedStatement = null;
        Map map = new HashMap();
        String add_supplemental = "SELECT * FROM residence_tb WHERE ri_id = ?"; 

            preparedStatement = conn.prepareStatement(add_supplemental);
            preparedStatement.setString(1,id);
            out.println("before");
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                    String lname = rs.getString("ri_lname");
                    String fname = rs.getString("ri_fname");
                    String mname = rs.getString("ri_mname");
                    String suffix = rs.getString("ri_suffix");  
                    String bdate = rs.getString("ri_bdate");
                    String age = rs.getString("ri_age");    
                    String gender = rs.getString("ri_gender");
                    String status = rs.getString("ri_status");  
                    String mobileno = rs.getString("ri_mobileno");
                    String telephoneno = rs.getString("ri_telephoneno");    
                    String province = rs.getString("ri_province");  
                    String municipality = rs.getString("ri_municipality");  
                    String brgy = rs.getString("ri_brgy");  
                    String street = rs.getString("ri_street");  


                    map.put("lname", lname);
                    map.put("fname", fname);


            }

          out.println(map); 
//        rst.close();
//        stmt.close();

        conn.commit();
        conn.close();

    } catch(Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
%>

It look good already when I have out.println(map); I get

{fname=Juan, lname=Dela Cruz}

In the response. When I tried return map i get

incompatible type : unexpected return value

How can I return map so I can use it in success or done functions in ajax

chrki
  • 6,143
  • 6
  • 35
  • 55
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69

1 Answers1

0

Please just return a JSON string from the response. JSON is the map in Browser.

Map<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put( "Status", "Success" );
jsonMap.put( "Rows", 100 );
ObjectMapper mapper = new ObjectMapper();
mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);        
String jsonStr = mapper.writeValueAsString( jsonMap );
response.getOutputStream().print( jsonStr );

ObjectMapper is a class for Jackson/fastxml. This code will return the JSON to the browser.

response.getOutputStream().print("{\"result\":[{ \"lname\": \"Smith\", \"fname\": \"John\"}, { \"lname\": \"Smith1\", \"fname\": \"John1\" }, { \"lname\": \"Smith2\", \"fname\": \"John2\" }]}");

At the browser:

var res = JSON.parse(request.responseText)
console.log(res.result);
YouXiang-Wang
  • 1,119
  • 6
  • 15
  • what do i need to import to use `ObjectMapper` and `JsonGenerator` o get error both – Brownman Revival Jul 28 '16 at 06:36
  • You need to download fastxml 2.5.x. If the return value is not complex. You could write the JSON format manually. response.getOutputStream().print( "{\"Status\":\"Success\"}" ); – YouXiang-Wang Jul 28 '16 at 06:38
  • for example i only want `[{ "lname": "Smith", "fname": "John", }, { "lname": "Smith1", "fname": "John1", }, { "lname": "Smith2", "fname": "John2", }]` can you show a sample? i just want to return this kind of data to ajax that is all.. from the select statement i made – Brownman Revival Jul 28 '16 at 06:43
  • i get error in `ObjectMapper` and `JsonGenerator` i used the updated code.. how to fix it? – Brownman Revival Jul 28 '16 at 07:07
  • i got it now i need to add the jars... only one problem i dont know which jar to add to get rid of problems on `mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);` i got problem with `getFactory` amd `JsonGenerator` i fixed the problem for `ObjectMapper` i added jackson jar and it is gone – Brownman Revival Jul 28 '16 at 08:02