2

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

2 回答 2

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

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

"NetworkError: 404 Not Found - http://localhost:8080/{ApplicationRoot}/BookList.vw/2"
于 2014-03-19T12:45:04.600 回答
0

我和你有同样的问题。我使用 Spring Boot 和 Spring Security。默认情况下,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 Token。如果您使用的是 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);
});
});
于 2017-04-02T06:05:51.770 回答