weixin_33696822 2016-02-12 21:24 采纳率: 0%
浏览 5

通过本地ajax发送json

I know that there is a lot of similar questions, but none of them helps me.

on server i hav method:

@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) {

    }

and on the clien side i send a request:

        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'
        }));

requests end with error 400(bad request) and i dont know why

  • 写回答

1条回答 默认 最新

  • 斗士狗 2016-02-12 21:56
    关注

    You need to use @RequestBody annotation in method parameters. For more info see, for example, here: Parsing json into java objects in spring-mvc

    评论

报告相同问题?