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

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

Passing parameter from Javascript to API in Controller

问题

我在尝试从 JSP 页面传递参数到控制器时遇到了一些问题。以下是我调用控制器中 API 的 JavaScript 代码:

    function help(value){
        // 需要将 "value" 参数传递给下面的 API
		window.open("<c:url value='doWavierGuide.do'/>", 'info', 'width=640,height=480,resizable=yes,scrollbars=yes');
	}

而我的控制器代码如下:

    @RequestMapping(value = "/doWavierGuide.do", method = RequestMethod.GET)
	public ModelAndView showWavierGuide() {
		Map<String, Object> modelMap = new HashMap<>();
		
		log.debug("showWavierGuide():方法为");
        // 需要将从 JavaScript 传入的值放在这里,暂时硬编码
		modelMap.put("method", "1");
		
		return new ModelAndView("wavierGuide", modelMap);
	}

但我不太清楚如何将 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:

function help(value){
    // need to pass the &quot;value&quot; parameter to this following API
	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;)
}

And my controller as such:

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

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,像这样:

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

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

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

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

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

And in the controller, retrieve the parameter using HttpServletRequest:

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

答案2

得分: 0

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

const cnn = async (path, options) => {
   const url = "www.example.com";
   let result = await fetch(`${url}${path}`,
       options
   );

   if (result.status >= 200 && result.status <= 299) {
       return await result.json();
   }

   return { data: null, error: true } 
}

// 向API发起请求
cnn("doWavierGuide.do", { method: "GET", body: {name: "your_name"} })
  .then(response => response.json())
  .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:

const cnn = async (path, options) =&gt; {
   const url = &quot;www.example.com&quot;;
   let result = await fetch(`${url}${path}`,
       options
   );

   if (result.status &gt;= 200 &amp;&amp; result.status &lt;= 299) {
       return await result.json();
   }

   return { data: null, error: true } 
}

// request to the API
cnn(&quot;doWavierGuide.do&quot;, { method: &quot;GET&quot;, body: {name: &quot;you_name&quot;} })
  .then(response =&gt; response.json())
  .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:

确定