title: How to access model attribute in Javascript
body: <p>I want to access a model attribute in Javascript. I use the following code:</p>
  
  <pre><code>model.addAttribute("data", responseDTO);
  </code></pre>
  
  <p>My DTO class:  </p>
  
  <pre><code>public class ResponseDTO {
  
      private List&lt;ObjectError&gt; errors;
  
      private Boolean actionPassed;
  
      private String dataRequestName;
  
      // and setter getter for all fields
  }   
  </code></pre>
  
  <p>I tried accessing the DTO using:</p>
  
  <pre><code>var data = "${data}";
  </code></pre>
  
  <p>But it is giving me a string representation of responseDTO instead, i.e <code>com.req.dto.ResponseDTO@115f4ea</code>. I can successfully access a field inside the DTO using:  </p>
  
  <pre><code> var data = "${data.actionPassed}";  
  </code></pre>
  
  <p>But this is not working for the <code>errors</code> attribute inside the DTO, as it is a <code>List</code> of <code>ObjectError</code>. How can I get complete <code>responseDTO</code> object in Javascript?   </p>
  
  <p>Thanks! </p>
  
  <hr>
  
  <p><strong>EDIT :</strong>     </p>
  
  <p>Initially I was using <code>jquery.post</code>     </p>
  
  <pre><code>$.post('ajax/test.html', function(data) {
    // Here I was able to retrieve every attribute even list of ObjectError.
  });
  </code></pre>
  
  <p>Now I want to remove Ajax and want to convert it into non-ajax approach (because of some unavoidable reasons). So I am doing a normal form submit and want load same form again and trying to load <code>data</code> model attribute in Javascript so that I can keep the rest of the code as it is.<br>
  I was wondering if it can be achieved in Javascript as it is doable using Jquery post?  </p>
  
  <hr>
  
  <p><strong>EDIT 2 :</strong>      </p>
  
  <p>I tried (Thank you @Grant for suggestions)  </p>
  
  <pre><code>JSONObject jsonObject =JSONObject.fromObject(responseDTO);
  String jsonString = jsonObject.toString();
  model.addAttribute("data",jsonString);    
  </code></pre>
  
  <p>and in Javascript  </p>
  
  <pre><code>var data = eval('('+ ${dataJson} +')');   // Getting error on this line  
  alert(data.actionPassed);   
  </code></pre>
  
  <p>But getting error and no alert is displayed<br>
  Error :<br>
  <img src="https://i.stack.imgur.com/ep7BQ.png" alt="enter image description here"></p>