2014-03-19 101 views
2

請理解,幫助我。所有做的是不錯,但一個方法是不行的,我有一個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" 

回答

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

我覺得這個網址爲n OT改正它可能是這樣的:

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

URL是正確的。其他部分是完美的工作 –

0

我有同樣的問題你。我使用的彈簧引導與Spring Security.By defualt,春季安全將使CSRF保護,使用PATCH的所有請求,POST,PUT,和/或DELETE將受到保護。因此,簡單的方法就是diseable的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); 
}); 
});