0

In a grails view I need to call a javascript method to get some info and so I have a submit action like this:

<input type="submit" name="submit" class="submit action-button" value="Generar" onclick="generateReport()" style="float: right" />

and at the end of the generateReport() I need to call/redirect to the show action of a Controller (because I'm at the create action already)

I'have tried with

1) var jSon = generateJSON();
    <g:remoteFunction controller="report" action="show" params="[data:jSon]" />

2) var jSon = generateJSON();
    <g:remoteFunction controller="report" action="show" params="[data:${jSon}]" />

1) data reaches null 2) compile error:

org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message
Attribute value quote wasn't closed (controller="report" action="show" params="[data:${jSon}]").
1
  • 1
    You shouldn't be using remoteFunction or any of the remote* tags. They are old, difficult to work with, and no longer supported in newer versions of Grails. You need to learn to make your own ajax calls using jquery/javascript. Commented Oct 22, 2015 at 2:11

3 Answers 3

1

you can try this..

declare a variable in your gsp like this..

<script type="text/javascript">
            var yourVariable = "${createLink(url: [controller: 'yourController', action: 'yourAction'])}";
</script>

then in your js file..you can use ajax.

example ajax

function checkUsernameAvailable(user, example){
    $.ajax(
    {
        url: yourVariable, <- this variable take from your gsp
        contentType:"text/json",
        type: "get",
        data: ({ id: usernamenya}),
        dataType: "json",
        cache: false,
        async: false,
        success: function(data) {
               //do something here
        },
        error: function(xhr) {
        }
    });                 
}
Sign up to request clarification or add additional context in comments.

Comments

0

var json cannot be assigned to data as params in , as var jSon is a javascript variable. Data in params attribute requires model parameter coming from controller.

In 1st option when it is assigned as params as it is not a model parameter from controller its showing null.

2nd option would not work as this is not the way to define model parameter from controller so giving compile error.

So you should better try getting jSon as model from controller and then define as params.

Or you can follow this link to define g:remoteFunction in onclick itself

http://grails.github.io/grails-doc/2.2.1/ref/Tags/remoteFunction.html

Hope this helps! Thanks.

Comments

0

The remoteFunction tag won't work for your situation because it's rendered into html and JavaScript (server side) before the value of jSon is known (client side).

You'll have to do the ajax call on your own (ex. Jquery).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.