1

I have a controller code like this :

def test23() {
        //params.id23 is been passed via ajax calls
        def find = User.findByUsernameLike("${params.id23}")
        println find
        render """<script type="text/javascript"> alert("hi"); </script>""" 
    }

This code is called using jquery's ajax method. Well, it works well, that is , I get output printed on my console, but the render part is not working (I'm not getting the alert).

What I done is correct? Or anything else need to be passed to render those scripts?

7
  • 6
    shudder... view code in the controller ;-) -- As a more helpful comment, you should really consider not doing this in the controller, and moving it into the view. So the AJAX requests a value, receives a Json object containing 'hi' and then calls alert or something. Or look at templating frameworks in javascript... It really depends what you're trying to achieve... Commented Feb 29, 2012 at 14:05
  • @tim_yates : I just want to know whether that works, wont put in my code though. But even though render should show me a alert, it doesn't that's why asked here :) Commented Feb 29, 2012 at 14:09
  • @tim_yates: thanks for that advice. I need to just append some data's to the DOM (i.e to some id elements so that user can see it), after user selecting a value from drop-down list box :) Commented Feb 29, 2012 at 14:18
  • 4
    in that situation, I'd render a map as JSON, and deal with attaching it to the DOM client-side Commented Feb 29, 2012 at 14:22
  • @tim_yates: I have a quick question, I heard of JSON, but haven't tried it. Lets say I want attach a div to DOM, div has only one line content in it. In that case to, using a JSON matters? Commented Feb 29, 2012 at 14:32

1 Answer 1

4

try this instead:

render text: """<script type="text/javascript"> alert("hi"); </script>""",
        contentType: 'js'

or possibly:

render text: """<script type="text/javascript"> alert("hi"); </script>""", 
        contentType: 'text/javascript'
Sign up to request clarification or add additional context in comments.

1 Comment

I am with Tim on this, returning script tag is not very MVCish. You should definitely return JSON or an html fragment from a template.

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.