我写了 1 个 web 应用程序,我尝试将 1 个 JSON 发送到服务器并将其保存到 MySQL 中。我遇到了这个问题:
POST http://localhost:8080/jsonspringhibernateexample/addclass [HTTP/1.1 404 Not Found 910ms]
这是我的 Controller (用于向 Mysql 添加一个类):
@Controller
public class ClassController {
@Autowired
private ClassService classService;
@RequestMapping(value ="/index",method = RequestMethod.GET)
public String getIndex(ModelMap map){
map.put("listClass",classService.getListClass());
map.put("studentClass", new Class());
return "class";
}
@RequestMapping(value ="/addclass", method = RequestMethod.POST)
public String addClass(@RequestBody Class studentClass){
classService.addClass(studentClass);
return "index";
}
}
这是我调用 Controller 的 jquery:
$("#addclass").click(function(e){
$.ajax({
url:"addclass",
type: "post",
contentType: 'application/json',
data: JSON.stringify({className:$("#input").val(),listStudent:null}),
}).done(function(){
$("#listclass").appent("<option>"+$("#input").val()+"</option>");
});
});
当我单击“#addclass”按钮时,类已添加但 Controller 未返回 index.jsp,我遇到了麻烦。 为什么我会收到该错误?我该如何解决? 谢谢!
最佳答案
在你的 Controller 类之前你有一个@RequestMapping(value="/jsonspringhibernateexample")
吗?
否则尝试删除 headers = {"Content-type=application/json"}
,也许内容类型是错误的(但我不确定)。
关于java - 未找到 HTTP/1.1 404 - JSON Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450686/