我有一个使用 Spring 构建的小型 Web 应用程序,并且可以从curl 和 postman 访问 REST 服务。如果我尝试使用 Web 应用程序上托管的 JavaScript 访问它们,则会收到 405 错误。什么可能导致这种情况?我意识到代码中存在不安全性,这只是一个仓促的课程项目,所以它们并不重要。如果我复制 JavaScript 代码并在浏览器的控制台中运行它,它就会起作用。用户等级import org.springframework.data.annotation.Id;public class User { @Id public String id; private String username; private String firstName; private String lastName; private String password; private int zipCode; private int jacketThreshold; private int coatThreshold; public User() {} public User(String username) { this.username = username; } public User(String firstName, String lastName, String password) { this.firstName = firstName; this.lastName = lastName; this.password = password; } public User(String username, String password, int zipCode) { this.username = username; this.password = password; this.zipCode = zipCode; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getZipCode() { return zipCode; } public void setZipCode(int zipCode) { this.zipCode = zipCode; } public int getJacketThreshold() { return jacketThreshold; } public void setJacketThreshold(int jacketThreshold) { this.jacketThreshold = jacketThreshold; }
3 回答
aluckdog
TA贡献1847条经验 获得超7个赞
检查您是否返回 @ResponseBody 或 @ResponseStatus
要解决此问题,只需添加 @ResponseBody
@RequestMapping(value="/user", method = RequestMethod.POST)
public @ResponseBody String updateUser(@RequestBody User user){
return userService.updateUser(user).getId();
}
或您的方法的@ResponseStatus。
@RequestMapping(value="/user", method = RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public String updateUser(@RequestBody User user){
return userService.updateUser(user).getId();
}
森林海
TA贡献2011条经验 获得超2个赞
跨源必须包含您的 js 托管服务器地址,例如 Ex - @CrossOrigin(origins="http://localhost:3000) 并将其添加在 @RestController 之后
添加回答
举报
0/150
提交
取消