請理解,幫助我。所有做的是不錯,但一個方法是不行的,我有一個404錯誤 我有幾個要求Spring MVC ajax DELETE方法404錯誤
function deleteFunc(id) {
$.ajax({
dataType: "json",
type: "DELETE",
url: "/BookList.vw/" + id,
async: true,
success: function (response) {
},
error: function (e) {
alert("Book doesn't found");
}
});
}
function modifyFunc(id) {
alert(id);
$.ajax({
dataType: "json",
type: "PUT",
url: "/EditBook.vw",
data: id,
success: function (response) {
},
error: function (e) {
alert('Server problems. You cannot modify this book.');
}
});
}
控制器:
@Controller
@RequestMapping("/BookList.vw")
public class BookListController {
@Autowired
private IBookService bookService;
public String getModelName() {
return "BookList";
}
public BookListController() {
}
@RequestMapping(method = RequestMethod.GET)
protected ModelAndView openMain(Model m) throws Exception {
m.addAttribute("book", new Book());
Map<String, Object> model = new HashMap<String, Object>();
List<Book> books = bookService.listBooks();
model.put("books", books);
return new ModelAndView(getModelName(), "model", model);
}
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
public ModelAndView delete(@PathVariable int id) throws Exception {
bookService.removeBook(id);
return new ModelAndView(getModelName());
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView search(@ModelAttribute Book b) throws Exception {
List<Book> books = bookService.searchBook(b.getName().trim());
Map<String, Object> model = new HashMap<String, Object>();
model.put("books", books);
return new ModelAndView("BookList", "model", model);
}
}
搜索和主要方法工作是好的,但我不明白爲什麼我有這樣的DELETE方法的錯誤:
"NetworkError: 404 Not Found - http://localhost:8080/BookList.vw/2"
URL是正確的。其他部分是完美的工作 –