我得到一個 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.
的JQuery/Ajax的POST調用,不支持的媒體類型
奇怪的是,如果我看POST調用螢火蟲,他們表示不會全成,但如果我做了一個「重發」,他們被髮送並且回答被檢索。
我已經嘗試接受的答案有:jquery ajax rest call - Unsupported Media Type - 我沒有工作。
-edit:如果有幫助,我使用莫西。
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
},
});
這是我的模型:
@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;
}
}
,這是我的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");
}
}
對不起,也沒有改變任何東西。 – User9123