Home Issue with redirect vs render from a ajax jquery call
Reply: 0

Issue with redirect vs render from a ajax jquery call

Vaibhav Karani
1#
Vaibhav Karani Published in 2017-12-01 00:30:06Z

This is what my js looks like,

$.ajax({
    url: "/traffic/validateParams",
    data: JSON.stringify(data),
    type: 'post',
    contentType: 'application/json; charset=utf-8',
    success: function (d) {
        if(d == "") {
            $.ajax({
                url: "/traffic/update",
                data: JSON.stringify(data),
                type: 'post',
                contentType: 'application/json; charset=utf-8'
            });
        } else {
            alert(d);
            btn.removeAttr("disabled");
        }
    }
});

Here, validateParams will return an error message if something is incorrect with the validation of the data, or else update method is called.

My issue starts here, with the update method. Below are two codes for the same method, the one with redirect doesnt work, and the one with render works just fine.

Code with redirect:

def update() {
    def p = (request.JSON ?: params)?.subMap(TrafficSimulatorConstant.params.keySet())
    TrafficSimulatorConstant.updateParams(p)
    log.info("traffic simulator configured")
    redirect(action: "status") }

Here, in the above code, even though the values are update, there is no redirect to the status page. and also, the button is not enabled.

Code with render:

def update() {
    def p = (request.JSON ?: params)?.subMap(TrafficSimulatorConstant.params.keySet())
    TrafficSimulatorConstant.updateParams(p)
    log.info("traffic simulator configured")
    String url = createLink(action:"status")
    render(text: "window.location.href='${url}'", contentType: "application/javascript") }

Here, the values are update and the page is redirected to the status page. The button enable/disable doesn't matter because it is a page redirect and we have a new page.

I would like to understand the difference between the two implementations and also would like to know if there is a way to achieve the same behavior with redirect?

Thanks in advance.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298912 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO