0

I am trying to access a Spring Model attribute from js and html. I have followed the below link and tried everything it said, but I keep getting errors:
How to access model attribute in Javascript

This is the code which I have:

     MyVOClass.java  

     private String id; 
     private String name; 
     //getters setters here 

     MyController.java 
     @RequestMapping(value = "/Display", method = RequestMethod.GET, headers 
      = "Accept=text/html")
      public String Display(@RequestParam(value = "id", required = true) 
      String id, @RequestParam(value = "name", required = true) String name, 
       Model model) {

      //set the values to the VO Object 
      MyVOClass myVo = call to another class to set the values 
      model.addAttribute("myVo ", new Gson().toJson(myVo));

      return "html page name "; 


      Js file: 
      var ID = JSON.parse('${myVo.id}');  

I am getting the below error on the console:

     angular.js:14525 SyntaxError: Unexpected token $ in JSON at position 0
     at JSON.parse (<anonymous>)

EDIT 2:

If I try

      var ID = JSON.parse(${myVo.id}); 

I get the following error:

      Uncaught SyntaxError: missing ) after argument list
      Uncaught Error: [$injector:modulerr]  

Im not sure what am I missing? Do i need any additional config in my js and html to access Model?

EDIT 1:

Adding my Java API code as well:

     @RequestMapping(value = "/display", method = RequestMethod.GET, headers 
     = "Accept=text/html")
     public String display(@RequestParam(value = "param1", required = true) 
     String param1, Model model) {

    String html = "/view";
    MyVo myVo = new MyVo();
    //set data 

    Gson gson = new Gson();
    String respJson = gson.toJson(authresp);

    String respstr = authrespJson.toString();
    model.addAttribute("myVo ", respstr );
    System.out.println("response Json string "+respstr );

    return html;
    }
sva
  • 37
  • 6
  • Could you add the JSON contents of `${myVo.id}` to your post please? – admcfajn Oct 19 '17 at 23:20
  • Hi @admcfajn, how I can do it? since the exception gets thrown on that line. – sva Oct 20 '17 at 17:37
  • In JS: `console.log(${myVo.id});` the right-click inspect-element and view the console. Not sure how to do it in Java. You'll probably want to print/echo/output it in Java before it gets to the js as the problem could be happening in the Java code, not the JavaScript code. – admcfajn Oct 20 '17 at 17:59
  • you'll also want to comment out the line which is throwing the exception while you inspect the json. – admcfajn Oct 20 '17 at 19:04
  • From Java, the response is populated properly. In the JS end If I try console.log(${myVo.id}); it throws an error Unexpected Token in the js end. – sva Oct 20 '17 at 19:19
  • Not able to get the json itself in the js. Let me post my API as well. – sva Oct 20 '17 at 19:20
  • This is the JSON, then? `myVo.toString()` – admcfajn Oct 20 '17 at 21:38
  • Yes, That is populated and well formed. – sva Oct 20 '17 at 21:46
  • 100% valid? I've updated my answer below, could you see if that works? – admcfajn Oct 20 '17 at 23:05
  • Here is the response JSON value from the System.out.println statement: {"id":"123412","name":"abc"} I just need to access id from my js. – sva Oct 24 '17 at 18:26
  • I'm sorry, but I need to withdraw from this conversation. I have a bunch of work to do. Good luck! – admcfajn Oct 24 '17 at 18:32
  • You might want to check the headers on the api's response to make sure they're application/json – admcfajn Oct 24 '17 at 18:37
  • I think you are confusing many things with the question that I am asking. I am trying to access the json from the js file in spring mvc in the same package. – sva Oct 24 '17 at 18:40
  • If you have an 'Unexpected token' error in your json, it's generally because a) the api isn't responding correctly, or b) the json isn't parsing correctly. This can be because of character encoding, http-headers, malformed json-strings, etc... You'll need to fix the unexpected-token error before you can retrieve any data from the json. I program in PHP & javascript (not Java), so I'm not the best person to help you debug this. Good luck. – admcfajn Oct 24 '17 at 19:03
  • It is the part that json isnt parsing correctly but I am not able to debug why. Following your explanation, i removed the headers = "Accept=text/html" from my API and then tested the UI but I am still getting the same error angular.js:14525 SyntaxError: Unexpected token $ in JSON at position 0 at JSON.parse () – sva Oct 24 '17 at 19:57
  • Did you change this `JSON.parse('${myVo.id}');` to this `JSON.parse(${myVo.id});` That's the first step. – admcfajn Oct 24 '17 at 20:05

0 Answers0