tags:

views:

22

answers:

1

I have a JSP page that renders a block of HTML. In normal usage, I load the contents of the JSP using AJAX (specifically, using jQuery.load()) and insert it into the page. Now, I need to be able to load this block of HTML into a different domain, which means the same-source restrictions won't allow me to use "normal" AJAX.

This block gets included in multiple places, some of which will be on the same domain and some of which will be on alternate domains. I would prefer to have it continue to work the way it works currently unless a specific parameter is passed (in all likelihood, that would be the callback function that is passed for JSONP support).

My conceptual solution (thus far) is to output JSONP, with a single KEY/VALUE pair, having the complete HTML output as the VALUE.

The PROBLEM: I can't find any way to get the buffered output that is waiting to be sent when the JSP finishes rendering and modify it (in this case, to replace actual new lines with "\n". Without that, I get an Unterminated String Literal error when my JSONP function hits the first new line.

EXAMPLE:

<%@page contentType="text/html"%>
<%
    String callback = request.getParameter("callback");
%>
<% if(callback != null) { %>
    // JSONP Function call, defining Key/Value Pair
    // New lines break because JavaScript strings cannot cross lines
    <%= callback %>({"key":'
<% } %>

<div id="my_content">
    ...
</div>

<% if(callback != null) { %>
    '}) // End of JSONP Function Call
<% } %>
+1  A: 

The JSON Taglib library should do what you need. I haven't used it myself, but it's the right approach to the problem, something like this:

<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

<json:object>
<json:property name="key">

<div id="my_content">
    ...
</div>

</json:property>
</json:object>

You'll probably have to wrap the JSONP parenthesis around the result yourself.

skaffman
Looked at the library and convinced myself for some reason that it wasn't what I was looking for. It did exactly what I wanted. Thanks.
ebynum