最近写SpringBoot项目的时候用到了ajax
但是在我运行后报错了,如下:
Exception processing template “/addType”: Error resolving template [/addType], template might not exist or might not be accessible by any of the configured Template Resolvers
大概意思就是找不到/type这个url
Controller方法如下
@RequestMapping("/addType1")
public void addType1(Type type){
typeService.addType(type);
}
ajax代码如下
<script>
function add() {
var typeadd = $("#type1-add").val();
$.ajax({
url: "/addType1",
data: {'type':typeadd},
dataType: "JSON",
method: "POST",
success: function (data) {
alert("添加成功!");
}
});
}
</script>
我寻思这也没问题啊,但是后来百度过才发现,SpringBoot后端要想接受数据,Controller里@Controller换成@RestController,或者在方法上加@Responsbody的注解,不然无法被识别。