最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Grails - Calling controller and rendering template from Javascript method - Stack Overflow

programmeradmin0浏览0评论

How do I map calls to a Grails controller from a Javascript method? I see a method using PHP, but not with grails:

function getSelected(checkList)
        {
            var idList = new Array();
            var loopCounter = 0;
            //find all the checked checkboxes
            jQuery("input[name=" + checkList + "]:checked").each
            (
              function()
              {
                //fill the array with the values
                idList[loopCounter] = jQuery(this).val();
                loopCounter += 1;
              }
            );

            //call here

        }

Edit:

${remoteFunction(controller:"person", action:"runThroughAll", params:"[ids:idList]")}

How do I map calls to a Grails controller from a Javascript method? I see a method using PHP, but not with grails:

function getSelected(checkList)
        {
            var idList = new Array();
            var loopCounter = 0;
            //find all the checked checkboxes
            jQuery("input[name=" + checkList + "]:checked").each
            (
              function()
              {
                //fill the array with the values
                idList[loopCounter] = jQuery(this).val();
                loopCounter += 1;
              }
            );

            //call here

        }

Edit:

${remoteFunction(controller:"person", action:"runThroughAll", params:"[ids:idList]")}
Share Improve this question edited Mar 9, 2012 at 20:19 user82302124 asked Mar 9, 2012 at 18:24 user82302124user82302124 1,1435 gold badges32 silver badges57 bronze badges 2
  • Ryan - Are you trying to make an Ajax call to a grails controller? – JSager Commented Mar 9, 2012 at 18:30
  • Yeah - Ultimately, I'd like to make a controller method call that renders a template from the JS method using the IdList. – user82302124 Commented Mar 9, 2012 at 18:33
Add a ment  | 

1 Answer 1

Reset to default 6

So, I feel like there are sort of two things you're asking here. I'm going to tackle them both. First, how do you get the URL right for a call to a grails controller from JavaScript? In my GSP page (I do it in my main layout but whatever), I like to do this little trick:

<script>
myapp.url.root = "<g:resource dir='' file='' />" + "/";
</script>

That will give you the base root of your app wherever it's deployed. Then, you can build your URLs in JavaScript:

myurl = myapp.url.root + "path/to/controller"

Then make a jQuery ajax call using that url.

Then make sure that your controller is set up to respond to whatever URL pattern you've just expressed.

The second question appears to be, "how can I send back an HTML fragment"?

Inside the controller itself, take the parameters from the request, use it to figure out whatever you need, then render the gsp, passing in the model you've created. It will look something like this:

def show() {
   def data = [hypothesis : metadataService.getHypothesis(params.id) as JSON]
   render(view:"create", model:data)
}

Then in jQuery, your success handler will get as an argument the returned response, which you can then inspect/manipulate/add to the dom.

Hopefully all that made sense. If I glossed over something or didn't answer the question you were asking, let me know.

EDIT: For future reference, here is the javascript method rewritten which we arrived at in chat:

function getSelected(checkList){ 
var idList = $("input[name='" + checkList + "']:checked").map(function(){ return $(this).val(); }); 

$.ajax({ 
url: "/path/to/controller", 
type:"POST", 
data:{ids:JSON.stringify(idList)} 
success:mySuccessFunction 
}); 
}
发布评论

评论列表(0)

  1. 暂无评论