0

This is how I return my model object from the controller.

model.addObject("gameSchList", gameSchList);
return model;

I got my response as a JSON output because I used Spring JAXB implementation. This is the output I got from my Spring controller.

{
   "gameSchList":
   [
       {
           "team":
           [
               {
                   "alias": "A",
                   "logo": "A_LOGO"
               },
               {
                   "alias": "B",
                   "logo": "B_LOGO"
               }
           ],
           "cName": "AZ",
           "id": "58",
           "type": "game",
           "channel": "713",
           "time": "Sun 13:00PM",
           "status": "pregame"
       }
   ]

}

Now I want to access this gameSchList within document ready function. This is an external js file which I added to my homepage.

Now within that gameinfo.js, I created a ready function and tried to access gameSchList using the below code. I need this requirement within js file without using jstl.

$(document).ready(function(){
    var list = ${gameSchList};
    $.each(list, function( index, value ) {
        alert( index + ": " + value );
    });
});

When I call this controller it redirects to my homepage and executes this particular gameinfo.js, but it gives me this error:

SyntaxError: missing ; before statement

I tried it several ways, but no luck for me.

var list = ${model.gameSchList};
var list = "${model.gameSchList}";
1
  • 1
    Page source probably holds the answer; how does the line var list = ${modelSchList}; look after rendering the JSP page? Commented Mar 10, 2015 at 22:29

2 Answers 2

3

Here is one solution, that you can try. In your jsp declare the below which gets evaluated on server side (make sure its in jsp and not on external js file).

<script type="text/javascript">
 var responseObject= ${gameSchList};
</script>

Now you have access to responseObject on js and you can use it on your document.ready function. Hope this helps.

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

1 Comment

It works. I added above java script in jsp page and access the response object in external js file. So jsp page has sds <script type="text/javascript"> var responseObject= ${gameSchList}; </script>
0

On JSP page, which compiled on server we can declared JavaScript object only in JSON format in the with function which will convert the JSON content to JavaScript object when browser downloaded and compiled this script:

<script>
 var object = JSON.parse('${data}');
<script/>

other way it download JavaScript object through Ajax request from Server, the browser automatically compile JSON content received from server to the JavaScirpt object:

.....
 sendRequest("moderatorBuildingEntrancePage/updateBuildingLabel/ajax",sendData,
     function(event, request, settings) {
           var recievedObject = event.result 
....

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.