如何将 JSP 生成的 HTML 包装在 JSONP 包装器中?

发布于 2024-09-06 06:51:25 字数 938 浏览 0 评论 0原文

我有一个呈现 HTML 块的 JSP 页面。在正常使用中,我使用 AJAX(具体来说,使用 jQuery.load())加载 JSP 的内容并将其插入页面中。现在,我需要能够将此 HTML 块加载到不同的域中,这意味着同源限制不允许我使用“正常”AJAX。

该块包含在多个位置,其中一些位于同一域,一些位于备用域。我希望它继续以当前的方式工作,除非传递特定参数(很可能,这将是为 JSONP 支持传递的回调函数)。

我的概念解决方案(到目前为止)是输出 JSONP,使用单个 KEY/VALUE 对,将完整的 HTML 输出作为 VALUE。

问题:我找不到任何方法来获取 JSP 完成渲染并修改它时等待发送的缓冲输出(在本例中,用“\n”替换实际的新行。如果没有的话,我会得到当我的 JSONP 函数到达第一个新行时出现 Untermerated String Literal 错误示例

<%@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
<% } %>

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
<% } %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-09-13 06:51:25

JSON Taglib 库 应该可以满足您的需要。我自己没有使用过它,但这是解决问题的正确方法,如下所示:

<%@ 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>

您可能必须自己将 JSONP 括号括在结果周围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文