1

我知道有一些线程有同样的问题,但是我没有让它正常运行。我对此还是很陌生。

我有一个 JAX-RS 服务器正在运行:

GET方法有效。该POST方法没有。

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response post(Movie movie){
    System.out.println("In the POST method");

    String result = movie.toString();

    return Response.status(201).entity(result).build();

在我的 Oracle JET 客户端中,我想发表一篇文章:

addMovie = function(){
    console.log("post sent");
      $.ajax({
        type: "POST",
        url: "http://localhost:8080/MovieRestService/resources/movies",
        headers: {

            "Content-Type": "application/json"
        },           
        data:  
                    {
                        id: 2,
                        name: "test",
                        director: "test",
                        year: 234
                    },
        success: "success",
        dataType: 'application/json'
      });

它一直给我一个415 Unsupported Media Type错误。对我来说似乎有点奇怪的是,在响应标头中,内容类型是 text/htmlContent-Type: text/htlm

有人有解决方案吗?

编辑:

在网上进行大量搜索后,我终于设法找出真正的问题是什么...... Glassfish 4.1.1 似乎有一个错误,是在向我的服务器发帖时导致问题的原因......

4

1 回答 1

1

这应该工作

    var url = 'http://localhost:8080/MovieRestService/resources/movies';
    var sucessCallback = function(response) {...}
    var data = JSON.stringify({
                    id: 2,
                    name: "test",
                    director: "test",
                    year: 234
                });
    $.ajax({
        url: url,
        method: POST,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: data,
        cache: false,
        context: this,
    }).success(sucessCallback);
于 2016-06-06T10:58:46.983 回答