0

I have a java file by which I want to pass map something like : { id: 5, GPA: 5} to my jsp file using AJAX. I am using following code for this:

In my JAVA file:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}

In jsp file: --some extJS code--

Ext.Ajax.request({
            url :'assertion.htm',
            method  : 'POST',
            params: {
                    existingRule : nameField.getRawValue()
            },
            scope   : this,
            success: function ( response ) {
                alert(response.responseText);
            }

response.responseText is printing entire jsp file instead of printing id:5, GPA:5 Can anyone help me in this?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Sapan
  • 9
  • 5

2 Answers2

1
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}

This won't compile, you are missing a return statement.

This seems to be a Spring MVC controller, judging by the ModelAndView return type. My guess is that you are returning a JSP view instead of the JSON Object you want to return. See this previous question of mine for how to return a JSON Object from Spring MVC.

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I am sorry but I have added return statement ModelAndView mav = new ModelAndView("AssertionScreen","AssertRules", "A"); return mav; – Sapan May 04 '11 at 12:34
0

Your function does not return anything and this is correct so change it to void:

protected void handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200