JQuery/Ajax POST 调用,不支持的媒体类型

Use*_*123 5 ajax rest jquery post

我得到一个 Unsupported Media Type - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

奇怪的是,如果我查看 firebug 中的 POST 调用,它们被指示为不成功,但是如果我执行“重新发送”,它们会被发送并检索到答案。

我已经在那里尝试了接受的答案:jquery ajax rest call - Unsupported Media Type - 对我不起作用。

-edit:如果有帮助,我会使用 Moxy。

                var sent = "This is a test request.";
                var add = "testing..";
                var request = { sentence: sent, addition: add };
                $.ajax({

                 url: "http://localhost:8080/MyProject/Rest/Path/toPost",
                 type: "POST",
                 data: JSON.stringify(request),
                 contentType: "application/json; charset=utf-8", 
                 dataType: "json",
                     success: function(resultData) {

                          //do stuff
                     },

               });
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

@XmlRootElement(name = "request")
public class Request {

private String sentence; 
private String addition;

public Request() {
    this.sentence = "default";
    this.addition = "default";
}

public Request(String sentence, String add) {
    this.sentence = sentence; 
    this.addition = add;
}

   public String getSentence() {
       return sentence;
   }

   public String getAddition() {
       return addition;
   }

   public void setSentence(String sentence) {
       this.sentence = sentence;
   }
   public void setAddition(String addition) {
       this.addition = addition;
   }

}


@XmlRootElement(name = "answer")
public class Answer {

private ArrayList<String> lsit;
private String info;

public Answer() {
    this.list = new ArrayList<String>();
    this.info = "Good";
}


public Answer(ArrayList<String> list, String info) {
    this.list = list;
    thisinfo = info;
}


public void setInfo(String info) {
    this.info = info;
}
public String getInfo() {
    return info;
}

public ArrayList<String> getList() {
    return list;
}
public void setList(ArrayList<String> list) {
    this.list = list;
}

}
Run Code Online (Sandbox Code Playgroud)

这是我的 Servlet:

    @Path("/Path")
    public class TestServlet {

        @Path("/toPost")
        @POST
        @Consumes({MediaType.APPLICATION_JSON})
        @Produces({MediaType.APPLICATION_JSON})
        public Answer consume(Request request) {
            ArrayList<String> res = new ArrayList<String>();
            res.add(request.getSentence());
            return new Answer(res, "Good");
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sca*_*nQR 0

删除字符集作为 json 不支持字符集,也将接受设置为 json,因为您正在为客户端生成 json。

            $.ajax({      
                     url: "http://localhost:8080/MyProject/Rest/Path/toPost",
                     type: "POST",
                     data: JSON.stringify(request),
                     Accept : "application/json",
                     contentType: "application/json", 
                     dataType: "json",
                         success: function(resultData) {

                              //do stuff
                         },

                   });
Run Code Online (Sandbox Code Playgroud)