我写了一个webapp,我试图向服务器发送一个JSON并将其保存到MySQL中。我遇到了这个问题:
POST http://localhost:8080/jsonspringhibernateexample/addclass [HTTP/1.1 404 Not Found 910ms]
这是我的控制器(用于向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";
}
}
这是我的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"按钮时,类被添加了,但控制器没有返回index.jsp,我遇到了麻烦。为什么我会出现这个错误?我该怎么解决?谢谢
控制器类之前有@RequestMapping(value="/jsonspringhibernateexample")
吗?否则,尝试删除headers = {"Content-type=application/json"}
,可能内容类型错误(但我不确定)。