简体   繁体   中英

I am able to pass the data from Spring boot controller to html page, but how do I access the data in Java Script?

This is my Controller:

{@RequestMapping("/component")
public String component(Model model) {
    Class obj=new Class ();
    JSONObject data=obj.fetchData();
    model.addAttribute("message",  obj.getMessage());
    model.addAttribute("data", data);     
    return "component";
}}

I am able to print data in the view of component.html like this:

<p th:text=${data}></p>

but how do I get the data in Java Script? js_component.js is my source.

<script th:inline="javascript" src="js_component.js"></script>

I've already tried this code in js_component.js but it does not work.

/*<![CDATA[*/
var json=/*[[${data}]]*/ 'data';

var msg=/*[[${message}]]*/ 'message';

/*]]>*/

how do I get the data in Java Script?

Did you try to return "js_component.js" at your controller.

@RequestMapping("/component")
public String component(Model model) {
    Class obj=new Class ();
    JSONObject data=obj.fetchData();
    model.addAttribute("message",  obj.getMessage());
    model.addAttribute("data", data);     
    return "js_component.js"; <==
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM