**问题如图所示__**请问如何解决?
<%--
Created by IntelliJ IDEA.
User: 汪海洋
Date: 2023/8/14
Time: 12:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>评论区</title>
</head>
<body>
ID:<input type="text" name="id" id="id"/><br><br>
评论:<textarea name="comment" id="words"></textarea><br><br>
<button onclick="setDate()">发表评论</button>
</body>
<script type="text/javascript">
function setDate() {
const time = new Date();
const data = {
id : document.getElementById("id").value,
comment : document.getElementById("words").value,
time : time.toLocaleDateString()
}
var xhr =new XMLHttpRequest()
xhr.open("POST","/addComment",true)
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if(xhr.status === 200 && xhr.readyState === 4){
console.log("请求发送成功!")
}
}
console.log(JSON.stringify(data))
xhr.send(JSON.stringify(data));
xhr.timeout = 2000
xhr.timeout =function () {
alert("超时!")
}
xhr.onerror =function () {
alert("网络异常!")
}
}
</script>
</html>
Pojo类
package com.experience.entity;
public class Comment {
private int id;
private String comment;
private java.sql.Timestamp time;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public java.sql.Timestamp getTime() {
return time;
}
public void setTime(java.sql.Timestamp time) {
this.time = time;
}
}
Controller层
```java
package com.experience.controller;
import com.experience.entity.Comment;
import com.experience.service.Comment_add_service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class Comment_add_Controller {
@RequestMapping(value = "/addComment",method = RequestMethod.POST)
public void addComment(@RequestBody Comment comment){
System.out.println("获取到JSON字符串是"+comment.toString());
}
}
```
1.尝试去掉日期类型的数据,然后再次传参,有可能是日期格式不正确
2.重启服务器,看代码逻辑没啥问题,但是后端添加RequestBody注解需要重启服务器才能生效
【相关推荐】
这里主要用@RequestBody转换接受的JSON为对象,用@ResponseBody转换返回的对象为JSON。
有两种接受前端数据的方式,一种使用Map接受,一种使用实体类进行接收,使用Map接受的方法为:
@RequestMapping("/peopleSelect")
@ResponseBody
public People peopleSelect(@RequestBody Map<String,String> map) {
//使用map.get方法得到JSON中id对应的值
long id = Long.parseLong(map.get("id"));
//查找对应id的用户信息
People people = peopleService.getById(id);
//返回用户信息,要使用@ResponseBody将返回值转换为JSON
return people;
}
使用实体类接受的话,要求实体类中有对应的属性,如People中有id,name,age属性,只能接受键名为id,name,age的JSON(可以不全有,但不能有People中没有的属性),方法为:
@RequestMapping("/peopleSelect")
@ResponseBody
public People peopleSelect(@RequestBody People requestPeople ) {
//使用requestPeople.getId方法得到JSON中id对应的值
long id = requestPeople.getId();
//查找对应id的用户信息
People people = peopleService.getById(id);
//返回用户信息,要使用@ResponseBody将返回值转换为JSON
return people;
}