简体   繁体   English

使用Ajax发送字符串到控制器

[英]Sending a string with Ajax to controller

My knowledge of Spring boot is very low. 我对Spring Boot的了解非常少。 I have made a table in HTML, which when I click on, an event is sent to a Javascript function, in which I try to catch the Id (a string) of the cell and send it to the controller. 我已经用HTML制作了一个表,单击该表时,会将一个事件发送到Javascript函数,在该函数中,我尝试捕获单元格的ID(字符串)并将其发送给控制器。 I tried many examples from internet but none worked for me, can anyone help me please? 我尝试了很多互联网上的示例,但没有一个对我有用,有人可以帮我吗? The id is being caught correctly by mouse click, but I have a problem with sending. 鼠标单击可正确捕获ID,但是发送时出现问题。

This is my JSP 这是我的JSP

<c:forEach items= "${rooms2}" var="m" >
<tr>
<td style="background-color:OldLace">${m.day }</td>
    <c:choose>
    <c:when test="${m.hour1confirm==false}">
    <td style="background-color:green" id="${m.hour1}"  onclick=
    "content(this)" >${m.hour1 }</td>
...
<script >
//  <script type="text/javascript">

function content(elem)
{
//  $(document).ready(function() 
//  {
//     // PREPARE FORM DATA
//          var idt =elem.id;
//          console.log(idt);
//          // DO POST
//          $.post({
//              type : "POST",
//              url :"reservation2",
//              // contentType: "application/json",
//              data : JSON.stringify(idt),
//              dataType : 'json',
//                  })

//  });

         var idt =elem.id;
         console.log(idt);
         $.ajax({

             url:"reservation2",
                    type: 'POST',
                    data:  idt,             
                    dataType: "text",          
                    contentType: false,
                    mimeType: false,
                    success: function(response){ 
                        console.log(data);              
                        return false;     
                    } 
                });

}

</script>

this is controller 这是控制器

.
.
.
//    @RequestMapping(value = "/reservation2", method = RequestMethod.POST)
//    public String reservation2(@ModelAttribute("idt") String idt , Model model) {
//        return "Reservation2";}



   @RequestMapping(value = "/reservation2", method = RequestMethod.POST)
    public @ResponseBody
    String Submit(@RequestParam("idt") String idt) {
        // your logic here
        return "reservation2";
    }


    @RequestMapping(value = "/reservation2", method = RequestMethod.GET)
    public String reservation(Model model) {  
   List<Room> rooms2= new ArrayList<>();
   System.out.println(rooms2);      
   rooms2= x.findAll();
   model.addAttribute("rooms2", rooms2);   
   return "reservation2";
...

when I run I get this error in console : 当我运行时,我在控制台中收到此错误:

POST http://localhost:8080/reservation2 403 ()

Try changing your request to something like this 尝试将您的请求更改为这样的内容

$.ajax({
  url: "/reservation2", //added '/' in the beginning
  type: 'POST',
  data: idt,
  dataType: "text",
  contentType: "text/plain", //change
  //mimeType: false,   //remove
  success: function(response) {
    console.log(data);
    return false;
  }
});

and your controller to this 和你的控制器

@RequestMapping(value = "/reservation2", method = RequestMethod.POST)
public String reservation2(@RequestBody String idt) 
{
    // your logic here
    return "reservation2";
}

I believe you are not sending the right parameter along the way. 我相信您在此过程中并未发送正确的参数。 When you do the ajax post request, in the field data can you specify the name of the parameter you are sending? 当您执行ajax发布请求时,可以在字段数据中指定要发送的参数的名称吗?

Can you try this one 你能试试这个吗

$.ajax({

         url:"reservation2",
                type: 'POST',
                data:  {"idt": idt},             
                dataType: "json",          
                contentType: "application/json",
                success: function(response){ 
                    console.log(data);              
                    return false;     
                } 
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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