1

Is it possible in one JSP to make a jQuery ajax callback to another JSP and have data returned?

I am trying to make the ajax call to Page2.jsp in the $(document).ready call in Page1.jsp I am attempting to get JSON returned by "Page2.jsp"

I am running Tomcat locally to test. I am seeing the JSON printed to the console, but not returned to the original calling method in Page1.jsp

Any ideas?

Page1.jsp

$(document).ready(function(){

    $.ajax({
        url : 'Page2.jsp',
        dataType: 'json',
        success : function(json) 
        {
            var obj = jQuery.parseJSON(json);
        }
    });
    });

Page2.jsp

<%@page contentType="application/json; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%

    JSONObject json = new JSONObject();

            json.put("amount","55.00");
            json.put("tax","1.00");

            String jString = JSONObject.toJSONString(json);
            PrintWriter out = response.getWriter();
        out.println(jString);
    out.close();
%>
edcincy
  • 321
  • 2
  • 3
  • 14
  • You should make the second page a servlet. It contains only Java code. JSPs shouldn't contain any, and should be used to generate markup from beans stored in the request. To generate a JSON object, a servlet is what you need. – JB Nizet May 22 '13 at 18:17
  • is the "json" parameter empty? Is the call sucessful (maybe add error callback to see what happens)? In principle your code should work, maybe there is an issue with paths? – Piotr Kochański May 22 '13 at 18:38
  • Believe me, I know that I should use a servlet. Unfortunately I do not have access to add/use one.
    I'm trying to see if using JSPs will work. The callback does not seem to be successful. I have tried putting an alert in both the "success" and "error", but neither are called. It seems as if the callback is simply not happening, or the Page2.jsp is not correctly returning the JSON string.
    – edcincy May 22 '13 at 19:03
  • The problem is probably caused by the fact that you write to the response writer, which is already wrapped/replaced by the JSP writer. Simply use the implicit object `out`, created for you by the container, to write your output, and don't close it. Or simply use `<%= jString %>`. Look in the network tab of Firebug/chrome dev tools to see the response you get. – JB Nizet May 22 '13 at 22:09
  • for sake of completeness take a look at: http://stackoverflow.com/questions/2010990/how-do-you-return-a-json-object-from-a-java-servlet –  Jan 14 '14 at 15:48

1 Answers1

2

I tried the code in your question and the jQuery.parseJSON() code throw the following error: "SyntaxError: JSON.parse: unexpected character". On debugging I saw that the servlet code generated by tomcat includes out.write("\r\n"); I suspect that these character are causing the syntax error.

Nevertheless in the javascript I tried accessing the returned object using the dot notation without parsing it and I was able to so as if it were a JSON object. It appears that it is not necessary to parse the returned object.

The only modification I made to the JSP code was to remove the lines PrintWriter out = response.getWriter(); and out.close();

Alex Theedom
  • 1,604
  • 15
  • 16
  • Nice job! Thanks Alex. Your changes/suggestions worked perfectly. I appreciate your taking the time to help. – edcincy May 30 '13 at 17:06