0

I have create a page and submit a post request to server, the jquery code like this

jquery code:

$("#button").click(function() {

    var book = {
        name: "book123",
        isbn: "123456"
    };

    $.post("book/add", book, function(data) {
        console.debug(data);
    });

});

Spring controller code:

@RequestMapping(value = "/add",method = RequestMethod.POST)
public @ResponseBody String addBook(
        @RequestBody Book book, HttpServletResponse response) throws Exception {

    book.setRanking(DEFAULT_RANKING);
    Book returnbook = bookService.createBook(book);

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(returnbook);
}

But it has error, in firebug, I get this error code:

NetworkError: 415 Unsupported Media Type - /book/add

the requst header information is this:

Accept / Accept-Encoding gzip, deflate

Accept-Language en;q=0.8,en-us;q=0.5

Content-Length 24

Content-Type application/x-www-form-urlencoded; charset=UTF-8

DNT 1

User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0

X-Requested-With XMLHttpRequest

I think the content-type is not correct as in bold. How to resolve this issue? Thanks!

blackpanther
  • 10,998
  • 11
  • 48
  • 78
user3003466
  • 323
  • 2
  • 5
  • 15

1 Answers1

1

You could use jQuery's ajax function instead of post, where you can specify the type. To see how to use it, check out this SO post Also it would be good to set the consumes variable on @RequestMapping to "application/json"

Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190