2

So I need to pass javascript variables into grails parameters to build and download a file. So initially did this with ajax just to learn that ajax doesn't do downloads. Initially this worked like so:

<script type="text/javascript" charset="utf-8">
    function myFunction() {

        jQuery.ajax({
            url: "Search/download",
            type: "POST",
            data: {facets: visualSearch.searchQuery.facets()}
        });
    }
</script>
<input type="button" onclick="myFunction()" value="download">

While this passed the mapping correctly, this didn't do downloads.

So now I am want to do something similar with a g:link

<g:link controller="Search" action="test" params="[facets: '\$(visualSearch.searchQuery.facets())']" >TEST GRAILS</g:link>

But all I get in the params in the controller are

facets=$(visualSearch.searchQuery.facets())
action=test
controller=search

How can I fix this to pass the facets (whether parsed or unparsed) into the controller. Thanks!

3 Answers 3

3

Adding your javascript variable in params will not work. The g:link is executed on the server side and has no knowledge of your javascript values. You can remove this params and instead add code on the `onclick' event of your link to set your javascript values in the params.

Something like: In the gsp page,

<g:link name="searchLink" controller="Search" action="test">TEST GRAILS</g:link>

and then in javascript (in the same page),

$(function() {
  $('a[name="searchLink"]').bind('click', function() {
    $(this).attr('href', $(this).attr('href') + '?facets=' + visualSearch.searchQuery.facets());
  })
})

Basically you are using Grails to generate the hyperlink and then using JQuery to append a query string with the parameters to that href

Hope that helps.

Sign up to request clarification or add additional context in comments.

6 Comments

what would the code be? just <g:link controller="Search" action="test" onclick="return exportList()" >TEST GRAILS</g:link> ?
sorry, still doesn't work, all the params has in it is action=test controller=search
there was a typo. should have been facet= See if that works for you. Also tested this snippet here - jsfiddle.net/hztYf
perfect! It worked, I had to use visualSearch.searchQuery.value instead of visualSearch.searchQuery.facets since otherwise I was getting back objects, but I should be able to parse it
Forcing the javascript to be on GSP when it doesn't need to be has a bad smell to it. Yes, it works. But it's kind of an anti-pattern in most cases.
|
1

What I tend to do in these cases is use the g:link to generate the URL, but then override the default behavior with jQuery to make the ajax call:

<g:link class="searchLink" controller="Search" action="test" params="[facets: '\$(visualSearch.searchQuery.facets())']" >TEST GRAILS</g:link>

$('.searchLink').on('click', function(e) {
  e.preventDefault(); // prevent default link behavior

  var $element = $(this);
  var url = $element.prop('href');

  $.post(url, function(data) { 
    // callback if you need it
  });
});

2 Comments

sorry a little confused, is the javascript meant to do the controller call or is it meant to get the facets?
the javascript makes the ajax call to the server using the href that is created via the g:link tag.
0

From what you have I think you should create a form and submit it in the click event:

<script type="text/javascript" charset="utf-8">
    function myFunction() {
        $('<form>', {
            "html": '<input type="text" name="facets" value="' + visualSearch.searchQuery.facets() + '" />',
            "action": '${createLink(controller: "Search", action: "test")}',
            "method": 'POST'
        }).appendTo(document.body).submit();
    }
</script>
<input type="button" onclick="myFunction()" value="download">

Or refactor your code to have a real html form and prepare the data for it before submit.

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.