从Javascript传递参数到控制器中的API。

huangapple go评论113阅读模式
英文:

Passing parameter from Javascript to API in Controller

问题

  1. 我在尝试从 JSP 页面传递参数到控制器时遇到了一些问题。以下是我调用控制器中 API JavaScript 代码:
  2. function help(value){
  3. // 需要将 "value" 参数传递给下面的 API
  4. window.open("<c:url value='doWavierGuide.do'/>", 'info', 'width=640,height=480,resizable=yes,scrollbars=yes');
  5. }
  6. 而我的控制器代码如下:
  7. @RequestMapping(value = "/doWavierGuide.do", method = RequestMethod.GET)
  8. public ModelAndView showWavierGuide() {
  9. Map<String, Object> modelMap = new HashMap<>();
  10. log.debug("showWavierGuide():方法为");
  11. // 需要将从 JavaScript 传入的值放在这里,暂时硬编码
  12. modelMap.put("method", "1");
  13. return new ModelAndView("wavierGuide", modelMap);
  14. }
  15. 但我不太清楚如何将 JavaScript 中的值传递给控制器。如果我在控制器中将参数值硬编码,页面可以显示。有什么想法吗?谢谢!
英文:

I was having some problem when trying to pass a parameter from jsp to controller. Here is the JavaScript where I call the API in Controller:

  1. function help(value){
  2. // need to pass the &quot;value&quot; parameter to this following API
  3. window.open(&quot;&lt;c:url value=&#39;doWavierGuide.do&#39;/&gt;&quot;, &#39;info&#39;, &#39;width=640,height=480,resizable=yes,scrollbars=yes&#39;)
  4. }

And my controller as such:

  1. @RequestMapping(value = &quot;/doWavierGuide.do&quot;, method = RequestMethod.GET)
  2. public ModelAndView showWavierGuide() {
  3. Map&lt;String, Object&gt; modelMap = new HashMap&lt;&gt;();
  4. log.debug(&quot;showWavierGuide() : Method is &quot;);
  5. // need to put the value passed in from javascript into here, temporary hardcoded
  6. modelMap.put(&quot;method&quot;, &quot;1&quot;);
  7. return new ModelAndView(&quot;wavierGuide&quot;, modelMap);
  8. }

But I not sure how can I pass the value from JavaScript to Controller. If I hardcoded the parameter value in Controller, the page managed to display. Any ideas? Thanks!

答案1

得分: 0

我成功解决了,通过修改 jstl 标签中的 URL,像这样:

  1. "<c:url value='doWavierGuide.do?method='/>" + value

然后在控制器中,使用 HttpServletRequest 检索参数:

  1. @RequestMapping(value = "/doWavierGuide.do", method = RequestMethod.GET)
  2. public ModelAndView showWavierGuide(HttpServletRequest request) {
  3. Map<String, Object> modelMap = new HashMap<>();
  4. modelMap.put("method", request.getParameter("method"));
  5. return new ModelAndView("wavierGuide", modelMap);
  6. }
英文:

I managed to solve it by changing the url in jstl tag like this:

  1. &quot;&lt;c:url value=&#39;doWavierGuide.do?method=&#39;/&gt;&quot;+value

And in the controller, retrieve the parameter using HttpServletRequest:

  1. @RequestMapping(value = &quot;/doWavierGuide.do&quot;, method = RequestMethod.GET)
  2. public ModelAndView showWavierGuide(HttpServletRequest request) {
  3. Map&lt;String, Object&gt; modelMap = new HashMap&lt;&gt;();
  4. modelMap.put(&quot;method&quot;, request.getParameter(&quot;method&quot;));
  5. return new ModelAndView(&quot;wavierGuide&quot;, modelMap);
  6. }

答案2

得分: 0

你需要使用一个库来向服务器发起HTTP请求,或者使用fetch来发送和接收数据。
我给出一个示例:

  1. const cnn = async (path, options) => {
  2. const url = "www.example.com";
  3. let result = await fetch(`${url}${path}`,
  4. options
  5. );
  6. if (result.status >= 200 && result.status <= 299) {
  7. return await result.json();
  8. }
  9. return { data: null, error: true }
  10. }
  11. // 向API发起请求
  12. cnn("doWavierGuide.do", { method: "GET", body: {name: "your_name"} })
  13. .then(response => response.json())
  14. .then(response => console.log(response))
英文:

You have to use a library to make http requests to the server, or use fetch to send and receive data.
I leave an example:

  1. const cnn = async (path, options) =&gt; {
  2. const url = &quot;www.example.com&quot;;
  3. let result = await fetch(`${url}${path}`,
  4. options
  5. );
  6. if (result.status &gt;= 200 &amp;&amp; result.status &lt;= 299) {
  7. return await result.json();
  8. }
  9. return { data: null, error: true }
  10. }
  11. // request to the API
  12. cnn(&quot;doWavierGuide.do&quot;, { method: &quot;GET&quot;, body: {name: &quot;you_name&quot;} })
  13. .then(response =&gt; response.json())
  14. .then(response =&gt; console.log(response))

huangapple
  • 本文由 发表于 2020年10月16日 11:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64382432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定