调用Java SpringBoot服务器上的方法,从网站上的JavaScript脚本。

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

Call a Java SpringBoot server method from a JavaScript script on a website

问题

<ul class="collapse list-unstyled" id="selectGameSubmenu">
    <li data-option="League of Legends">
        <a href="#"><img src="../../../static/images/league_of_legends/leagueoflegends_icon.png"
                         alt="" style="width:16px;height:16px"> League of Legends</a>
    </li>
    <li data-option="Teamfight Tactics">
        <a href="#"><img src="../../../static/images/teamfight_tactics/teamfighttactics_icon.png"
                         alt="" style="width:16px;height:16px"> Teamfight Tactics</a>
    </li>
    <li data-option="Legends of Runterra">
        <a href="#"><img src="../../../static/images/legends_of_runterra/legendsofruneterra_icon.png"
                         alt="" style="width:16px;height:16px"> Legends of Runterra</a>
    </li>
    <li data-option="Valorant">
        <a href="#"><img src="../../../static/images/valorant/valorant_icon.png"
                         alt="" style="width:16px;height:16px"> Valorant</a>
    </li>
    <li data-option="Counter Strike">
        <a href="#"><img src="../../../static/images/counter_strike/counterstrike_icon.png"
                         alt="" style="width:16px;height:16px"> Counter Strike</a>
    </li>
</ul>
$("#selectGameSubmenu").click(function(e){
    e.preventDefault();
    var option = $("#selectGameSubmenu li").data("option");
    console.log(option);
});
@PostMapping("/change-game")
public String changeGame(@RequestParam String game){
    System.out.println("Selected option: " + game);
    return "index";
}
英文:

I'm working on a submenu on my website, that has 5 options, each of them is name of a game. When user selects an option, I want my server to print a name of the game, that user selected. Here i have two problems:<br>

  1. I don't know why after click on a submenu option, I always get the
    first game name. <br>
  2. I don't know how to send a request from a JS to my Spring Boot application, which is a server. <br>
    Can you help me? Here is my code:
&lt;ul class=&quot;collapse list-unstyled&quot; id=&quot;selectGameSubmenu&quot;&gt;
    &lt;li data-option=&quot;League of Legends&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;../../../static/images/league_of_legends/leagueoflegends_icon.png&quot;
                         alt=&quot;&quot; style=&quot;width:16px;height:16px&quot;&gt; League of Legends&lt;/a&gt;
    &lt;/li&gt;
    &lt;li data-option=&quot;Teamfight Tactics&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;../../../static/images/teamfight_tactics/teamfighttactics_icon.png&quot;
                         alt=&quot;&quot; style=&quot;width:16px;height:16px&quot;&gt; Teamfight Tactics&lt;/a&gt;
    &lt;/li&gt;
    &lt;li data-option=&quot;Legends of Runterra&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;../../../static/images/legends_of_runterra/legendsofruneterra_icon.png&quot;
                         alt=&quot;&quot; style=&quot;width:16px;height:16px&quot;&gt; Leagends of Runterra&lt;/a&gt;
    &lt;/li&gt;
    &lt;li data-option=&quot;Valorant&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;../../../static/images/valorant/valorant_icon.png&quot;
                         alt=&quot;&quot; style=&quot;width:16px;height:16px&quot;&gt; Valorant&lt;/a&gt;
    &lt;/li&gt;
    &lt;li data-option=&quot;Counter Strike&quot;&gt;
        &lt;a href=&quot;#&quot;&gt;&lt;img src=&quot;../../../static/images/counter_strike/counterstrike_icon.png&quot;
                         alt=&quot;&quot; style=&quot;width:16px;height:16px&quot;&gt; Counter Strike&lt;/a&gt;
    &lt;/li&gt;
&lt;/ul&gt;
$(&quot;#selectGameSubmenu&quot;).click(function(e){
    e.preventDefault();
    var option = $(&quot;#selectGameSubmenu li&quot;).data(&quot;option&quot;);
    console.log(option);
});
@PostMapping(&quot;/change-game&quot;)
public String changeGame(@RequestParam String game){
    System.out.println(&quot;Selected option: &quot; + game);
    return &quot;index&quot;;
}

答案1

得分: 1

你的第一个问题出现在你的点击事件监听器下。所以每当你点击你的ul元素时,它都会触发,但是由于你定义了这样一个选择器 $(&quot;#selectGameSubmenu li&quot;)它总是会返回你的ul的第一个子元素,也就是League of Legends

要解决这个问题,你应该稍微修改一下。首先,不要在你的ul上添加事件监听器,而是在你的li上添加事件监听器,然后通过使用$(this)获取特定的点击项,输出应该像这样:

$(&quot;li&quot;).click(function (e) {
  e.preventDefault();
  var option = $(this).data(&#39;option&#39;);
  console.log(option);
});

你的第二个问题很简单,要在你的后端和前端应用程序之间进行通信,你应该使用类似于ajax的东西。ajax请求将有助于在应用程序之间检索和发送数据。关于它的更多信息,你可以阅读以下这些文章之一:JQuery文档Site point,或者Free code camp

你的请求应该像这样:

$.ajax({
  type: &quot;POST&quot;,
  url: url, // 你的终端地址
  data: data, // 你的数据
  success: success // 在数据成功发送后执行的操作
});
英文:

Your first problem lies under your click event listener. So whenever you click on your ul elements it will trigger but as you define such a selector $(&quot;#selectGameSubmenu li&quot;) it will always return your first child of ul which is League of Legends.

To fix it you should modify it a bit. First of all, instead of adding event listener in your ul you should add an event listener in your li and then get the specific clicked item by using $(this), the output should be something like this:

$(&quot;li&quot;).click(function (e) {
  e.preventDefault();
  var option = $(this).data(&#39;option&#39;);
  console.log(option);
});

Your second problem is simple, for communicating between your backend and frontend application you should use something like ajax. An ajax request will help to retrieve and send data between applications. For more information about it, you can read either of these articles: JQuery documentation, Site point, or Free code camp.

Your request will be something like this:

$.ajax({
  type: &quot;POST&quot;,
  url: url, // Your end point address 
  data: data, // Your data
  success: success // Your actions after data send successfully 
});

huangapple
  • 本文由 发表于 2020年5月4日 09:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61583736.html
匿名

发表评论

匿名网友

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

确定