简体   繁体   中英

Accessing Request Parameters in JavaScript

How do I access an object/parameter sent from my Spring Controller in a seperate JavaScript file? I am able to get the parameter in the JSP file but not in the JS file which is included with the JSP as a seperate * .js file.

Environment: Tomcat, Spring, JSP, JS

You cant get the parameter directly in Javascript, only in the JSP. This is because Javascript is executed client side and JSP is executed server side.

However, what you can do is get your JSP to output the value of the requestparameter in a JS variable.

Eg in your JSP file:

<script type="text/Javascript">

var properties = { }
properties.myParam = <%= request.getParameter("myParam") %>;

</script>

Then you can access these JS objects elsewhere in your JS.

I am not sure this will compile it is just giving you a rough idea.

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