我有一個簡單的jquery ajax調用休息服務。我將contentType設置爲「application/json」,其餘資源配置爲接受「MediaType.APPLICATION_JSON」。這是一種POST方法。 使用此設置,我得到「不支持的媒體類型」錯誤。jquery ajax休息電話 - 不支持的媒體類型
標題信息顯示 「內容類型的應用/ JSON;字符集= UTF-8」,在請求頭
響應顯示:狀態報告:不支持的媒體類型 服務器拒絕該請求因爲請求實體的格式不是所請求方法的請求資源支持的格式(不支持的介質類型)。
請提供一些解決此問題的指針。
這裏是代碼片段:
REST資源
@POST
@Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
@Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML})
public Response addPerson(MyJSONObj myObj) {
//...
// ...
//...
}
的jquery
$(document).ready(function() { /* put your stuff here */
$("#Button_save").click(function(){
var firstName = $('firstName').val();
var lastName = $('lastName').val();
var person = {firstName: firstName, lastName: lastName};
$.ajax({
url:'http://localhost:8080/sampleApplication/resources/personRestService/',
type: 'POST',
data: person,
Accept : "application/json",
contentType: "application/json",
success:function(res){
alert("it works!");
},
error:function(res){
alert("Bad thing happend! " + res.statusText);
}
});
});
});
集管作爲顯示在FF螢火蟲
響應頭
Content-Length 1117
Content-Type text/html;charset=utf-8
Date Thu, 05 Apr 2012 09:44:45 GMT
Server Apache-Coyote/1.1
請求頭
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Content-Length 97
Content-Type application/json; charset=UTF-8
Host localhost:8080
Referer http://localhost:8080/sampleApplication/
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0
X-Requested-With XMLHttpRequest
經過測試@Options,它似乎不工作,也必須在響應 – 2013-05-12 15:38:35
設置標題,你可以提供更多的信息,哪些錯誤發生?因爲我在我的所有項目中都使用這個代碼。 – 2013-05-15 12:59:13