Pass Javascript Variable to createelink call Grails method

var search= document.getElementById('appMenu').value document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch', params: ['query': search])}' 

The appMenu element is a text field, so I get the value that the user enters in the text field to go to the search controller. However, he continues to tell me that the params request is null. It seems that the search is not being passed to the link building method. Anyone have a suggestion?

+6
source share
1 answer

Grails (controllers, GSP and tags, etc.) work on the server side. JavaScript on the client side. And this link is prepared before sending data to the browser and before JavaScript can pass its variable to the GSP tag.

But you can prepare the base link on the server side and add an additional parameter on the client side using javascript, for example:

 var search= document.getElementById('appMenu').value; document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch')}?query=' + escape(search); 
+11
source

Source: https://habr.com/ru/post/918113/


All Articles