From my Controller i am passing a parameter to the view.
When i print the parameter from the Body it gets printed correctly. However, if i try to print the parameter from a Javascript function it doesn't work.
How can i correct this ?
Controller
def showpeople(Long id){
def pInstance = People.get(id)
render (view: "showpeople",model: [pInstance: pInstance])
}
View
<script>
$(document).ready(function(){
var str = ${pInstance.stringval}; // <--- DOESN'T WORK
alert (str); // <--- DOESN'T SHOW ALERT
)}
</script>
<body>
${pInstance.stringval} <--- PRINTS SUCCESSFULLy
</body>
The reason why your alert doesn't show is you are outputting that value as a constant instead of a valid String within javascript. You really need to wrap it in quotes and you should also encode the string value for use with javascript.
For example:
var str = '${pInstance.stringval?.encodeAsJavaScript()}';
alert(str);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.