【问题标题】:Spring MVC ajax DELETE method 404 errorSpring MVC ajax DELETE方法404错误
【发布时间】:2014-04-25 16:23:49
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:
    "NetworkError: 404 Not Found - http://localhost:8080/BookList.vw/2" 
    

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

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

    【讨论】:

      【解决方案2】:

      我和你有同样的问题。我使用 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);
      });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 2023-03-19
        • 2011-10-04
        • 1970-01-01
        • 2011-08-16
        • 2013-09-18
        • 2013-08-14
        相关资源
        最近更新 更多