2014-03-19 3 views
2

이해를 도와주세요. 다 모두 잘하지만 한 가지 방법이 작동되지 않고, 내가 몇 요청스프링 MVC 아약스 DELETE 메소드 404 오류

이 나는 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" 

답변

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

내가 URL을 n은 생각 OT는이 같은 수 있습니다 수정 :

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

URL이 맞습니다. 다른 부분은 완벽한 작업입니다. –

0

사랑해은, 스프링 시큐리티는 CSRF 보호를 가능하게 할 것이다 봄 Security.By를 만들면 기본적으로 봄 부팅을 사용하여 내가 같은 문제가 패치를 사용하여 모든 요청은 POST는, PUT, 및/또는 DELETE가 보호됩니다. 당신이 CSRF 보호를 유지하려는 경우 다른 방법에

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

, 당신은 CSRF 토큰은 모든 요청입니다 작성해야합니다 그래서, 간단한 방법은 diseable 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); 
}); 
});