Spring MVC ajax DELETE方法404错误

时间:2014-03-19 12:19:06

标签: jquery ajax spring-mvc http-status-code-404 spring-form

请理解,请帮助我。一切都做得很好,但有一种方法不起作用,我有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"

2 个答案:

答案 0 :(得分:0)

"NetworkError: 404 Not Found - http://localhost:8080/BookList.vw/2" 

我认为这个网址不正确可能是这样的:

"NetworkError: 404 Not Found - http://localhost:8080/{ApplicationRoot}/BookList.vw/2"

答案 1 :(得分:0)

我遇到了同样的问题。我使用Spring Boot和Spring Security.By defualt,Spring Security将启用CSRF保护,所有使用PATCH,POST,PUT和/或DELETE的请求都将受到保护。因此,简单的方法是CSRF保护无法解决,如:

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ...
        http.csrf().disable();
        ...
    }
    ...
}

另一方面,如果你想保持CSRF保护,你应该在每个请求中总结CSRF令牌。 如果您使用的是JSON,则无法在HTTP参数中提交CSRF令牌。相反,您可以在HTTP标头中提交令牌。典型的模式是在元标记中包含CSRF标记。 JSP的示例如下所示:

<html>
<head>
    <meta name="_csrf" content="${_csrf.token}"/>
    <!-- default header name is X-CSRF-TOKEN -->
    <meta name="_csrf_header" content="${_csrf.headerName}"/>
    <!-- ... -->
</head>
<!-- ... -->

然后,您可以在所有Ajax请求中包含令牌。如果您使用的是jQuery,可以使用以下命令完成:

$(function () {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader(header, token);
});
});