我正在为Web客户端编写一个评论框。
我有两个表,一个用于Comments
,另一个用于Users
。我想将HTML格式的<input>
标签中的输入数据发送到我的Java服务。在本服务中,我处理收到的数据。我现在的问题是,我收到HTTP-415错误而且我现在不知道该怎么做。
我在JavaScript中使用AJAX发布数据。
HTML
ID: <input type="text" name="id" value="1" readonly/> <br/>
Author: <input type="text" name="author"/> <br/>
comment: <input type="text" name="comment"/> <br/>
<button id="submit" type="Button" >Submit</button>
的Javascript
function addPost(givenID){
var $author = $("#author");
var $comment= $("#comment");
var $id = $("#id")
$("#submit").on("click", function(){
var post = $author.val()+"*"+ $id.val()+"*"+ $comment.val();
$.ajax({
type: "POST",
url: "api/kommentar",
data: post,
success: function(){
console.log("SUCCESS");
},
error: function(){
console.log("FAILIURE");
}
});
});}
爪哇
@Path("/kommentar")
public class KommentarService {
@EJB
private KommentarManagement postMgmt;
public KommentarService() { }
@POST
@Consumes("text/plain")
public void addPostsAsJson(String income) {
System.out.println(income);
//CODE TO HANDLE...
}
答案 0 :(得分:0)
jQuery的Ajax函数的默认内容类型是'application / x-www-form-urlencoded;字符集= UTF-8' 。 因为服务期望'text / plain',所以内容类型需要更改。
添加内容类型:
data: post,
contentType: 'text/plain', //<--- insert
success: function(){