2016-02-12 7 views
0

Я знаю, что есть много подобных вопросов, но ни один из них не помогает мне.отправить json через native ajax

на сервере я ВГ метод:

@RequestMapping(value = "/event", method = RequestMethod.POST , produces = MediaType.APPLICATION_JSON_VALUE) 
    public void bookOpened(
      @RequestParam(value="id", required = true) int id, 
      @RequestParam(value="type", required = true) String type, 
      HttpServletRequest request, HttpServletResponse response, HttpSession httpSession) { 

    } 

и на Clien стороны я послать запрос:

 var xhr = new XMLHttpRequest(); 
     xhr.open('POST', '/event'); 
     xhr.setRequestHeader('Content-Type', 'application/json'); 
     xhr.onload = function() { 
      if (xhr.status === 200) { 
       var userInfo = JSON.parse(xhr.responseText); 
       console.log(userInfo); 
      } 
     }; 
     xhr.send(JSON.stringify({ 
      id: 1, 
      type: 'BOOK_VIEWED' 
     })); 

запросов конца с ошибкой 400 (плохой запрос) и я не знаю, почему

ответ

1

Вам необходимо использовать аннотацию @RequestBody в параметрах метода. Для получения дополнительной информации см., Например, здесь: Parsing json into java objects in spring-mvc

Смежные вопросы