2

I am fairly new to Grails MVC and javascript. I am encountering a problem wherein i want to pass a map object from the controller to a javascript function. Currently two parameters are passed to the javascript function which are comma seperated and this works fine

eg. someControllerFunction() {

    variableLink = "j-javaScriptFunction-${stringArgs1},${stringArgs2}" // This is a link for an ajax call 

} 

The javascript function structure looks like this

function someJavaScriptFunction (details) {

    var d = details.split(",");
    var strintArgs1 = d[0];
    var stringArgs2 = d[1];

    ajax":{
                    "url":"${request.contextPath}/controller/methodInController?strintArgs1=" + strintArgs1 + "&stringArgs2=" +stringArgs2 
                    },
}

The Controller function which is called in the ajax currently looks like this

methodInController (String strintArgs1,String strintArgs2){

    //some operation
} 

Now i want to pass a map object from the controller function to the javascript function but i am not able to as the javascript considers the map as an invalid String object.

Below are the changes i have made to the three functions but i am getting an error during the ajax call saying "Uncaught SyntaxError: Unexpected string"

eg. someControllerFunction() {

    variableLink = "j-javaScriptFunction-${stringArgs1},${stringArgs2},${mapArg}" // This is a link for an ajax call 

} 

The map object looks like this

mapArg = [a:[],b:[],c:[],d:[]] 

The javascript function structure looks like this

function someJavaScriptFunction (details) {

    var d = details.split(",");
    var strintArgs1 = d[0];
    var stringArgs2 = d[1];
    var mapArg = d[2];

    ajax":{
                    "url":"${request.contextPath}/controller/methodInController?strintArgs1=" + strintArgs1 + "&stringArgs2=" +stringArgs2 + "&mapArg=" +mapArg 
                    },
}

The Controller function which is called in the ajax currently looks like this

methodInController (String strintArgs1, String strintArgs2, Object mapArg){

    //some operation
} 

It might be something to do with the way i am passing it to the javascript function but i am not able to figure out the exact reason. Could anyone please help me to understand what am i doing wrong. Thanks in advance

1 Answer 1

1

It looks like you might have to cast the map to JSON first (which would explain why its a string). See this answer: https://stackoverflow.com/a/2064341/1902587

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

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.