开发者

How do I configure ColdFusion Model Glue 3 so that it won't redirect on AJAX requests?

开发者 https://www.devze.com 2023-04-04 02:26 出处:网络
I have an event-handler, configured like so: <event-handler name=\"action.product.delete\"> <broadcasts>

I have an event-handler, configured like so:

<event-handler name="action.product.delete">
    <broadcasts>
        <message="DeleteProduct"/>
    </broadcasts>开发者_StackOverflow社区;
    <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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号