如何配置 ColdFusion Model Glue 3,使其不会重定向 AJAX 请求?

发布于 2024-12-03 18:33:00 字数 1190 浏览 0 评论 0原文

我有一个事件处理程序,配置如下:

<event-handler name="action.product.delete">
    <broadcasts>
        <message="DeleteProduct"/>
    </broadcasts>
    <results>
        <result do="page.product.list" redirect="true"/>
    </results>
</event-handler>

换句话说,删除产品,然后将用户重定向回产品列表。该事件将从另一个名为 page.product.delete 的事件调用,该事件显示删除确认页面。

现在我尝试使用 Model Glue 3 中引入的远程处理服务。我尝试发送一个 AJAX POST 请求:

$.ajax({
    url: 'RemotingService.cfc?method=executeevent&requestformat=json',
    data: {
        id: productId,
        eventName: 'action.iat.delete',
        returnValues: 'message'
    },
    type: 'POST'
});

虽然这在删除产品方面效果很好,但最终发生的情况是浏览器将发送一个 < strong>POST 请求,接收 302 重定向,然后立即执行 GET 请求。 GET 请求是 HTML 页面,而不是 JSON 数据。

如果我从事件处理程序中删除 redirect="true",AJAX 将正常工作,但从页面的非 AJAX 版本生成的 URL 将无法正常工作。最终发生的情况是用户将执行删除并确认,action.product.delete 页面将向他们显示 page.product.list 页面,但不更改 URL。如果用户将此页面添加为书签,他们将添加action.product.delete书签!

如何配置我的应用程序以便两者都能正常工作,或者我是否必须重新使用 CFC 上的远程过程调用来处理我的 AJAX?

I have an event-handler, configured like so:

<event-handler name="action.product.delete">
    <broadcasts>
        <message="DeleteProduct"/>
    </broadcasts>
    <results>
        <result do="page.product.list" redirect="true"/>
    </results>
</event-handler>

In other words, delete the product, then redirect the user to back to the products list. This event will be called from another event called page.product.delete, which shows a delete confirmation page.

Now I'm trying to use the remoting service that was introduced in Model Glue 3. I try to send an AJAX POST request:

$.ajax({
    url: 'RemotingService.cfc?method=executeevent&requestformat=json',
    data: {
        id: productId,
        eventName: 'action.iat.delete',
        returnValues: 'message'
    },
    type: 'POST'
});

Although this works fine in terms of deleting the product, what ends up happening is that the browser will send one POST request, receive a 302 Redirect, and then do a GET request immediately afterwards. The GET request is the HTML page and not JSON data.

If I remove the redirect="true" from the event-handler, the AJAX will work correctly, but the generated URL from the non-AJAX version of my page will not. What will end up happening is that the user will perform a delete and confirm it, and the action.product.delete page will show them the page.product.list page, but not change the URL. If the user bookmarks this page, they will be bookmarkingaction.product.delete!

How do I configure my application so that both will work properly, or will I have to go back to using remote procedure calls on CFCs to handle my AJAX?

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

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

发布评论

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

评论(1

日记撕了你也走了 2024-12-10 18:33:00

你们非常接近。您已经使用的 requestFormat 变量很特殊,因为它可以用作 Model-Glue 3 事件处理程序中的广播、视图和结果的过滤器。 requestFormat 的默认值是 html,因此,如果您为处理程序的结果块指定它,则您的 json 请求将跳过其中的结果:

<event-handler name="action.product.delete">
    <broadcasts>
        <message="DeleteProduct"/>
    </broadcasts>
    <results format="html">
        <result do="page.product.list" redirect="true"/>
    </results>
</event-handler>

如果您需要为您的 json 请求处理不同的结果,只需在事件处理程序中添加 ... 块即可。 Model-Glue 3 允许每个事件处理程序有多个广播、视图和结果块。

有关详细信息,请查看 Model-Glue wiki

You are very close. The requestFormat variable you are already using is special in that it can be used as a filter for broadcasts, views, and results within a Model-Glue 3 event handler. The default value for requestFormat is html, so if you specify it for your handler's result block the results in it will be skipped for your json requests:

<event-handler name="action.product.delete">
    <broadcasts>
        <message="DeleteProduct"/>
    </broadcasts>
    <results format="html">
        <result do="page.product.list" redirect="true"/>
    </results>
</event-handler>

If you need a different result to be processed for your json requests, just add a <results format="json">...</results> block in your event handler. Model-Glue 3 allows multiple broadcasts, views, and results blocks per event handler.

For more information check out the Formats page on the Model-Glue wiki.

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