2012-04-05 97 views
5

我有一個簡單的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 

回答

0

看起來你可以從漏水的抽象痛苦。看到這個迴應: JQuery's getJSON() not setting Accept header correctly?

如果你正在做一個跨域調用,那麼看起來你不能設置接受頭,因爲jQuery如何抽象你的調用。

儘管如此,您確實認爲服務器正在看到正確的接受標頭。這可能表明一個不同的問題。

2

我有同樣的問題,我是能夠解決這樣的說法(見http://www.weverwijk.net/wordpress/tag/jquery/):

$.ajax({ 
    url:'http://localhost:8080/sampleApplication/resources/personRestService/', 
    type:'POST', 
    data: JSON.stringify(person), 
    dataType: 'json', 
    contentType: "application/json; charset=utf-8", 
    success:function(res){ 
     alert("it works!"); 
    }, 
    error:function(res){ 
     alert("Bad thing happend! " + res.statusText); 
    } 
}); 

在Java方面我添加這些(見Access-Control-Allow-Origin):

@OPTIONS 
public Response testt(@Context HttpServletResponse serverResponse) { 
    serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); 
    serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); 
    serverResponse.addHeader("Access-Control-Allow-Origin", "*"); 
    serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With"); 
    serverResponse.addHeader("Access-Control-Max-Age", "60"); 
    return Response.ok().build(); 
} 

@POST 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(APPLICATION_JSON) 
public Response addPerson(MyJSONObj myObj, @Context HttpServletResponse serverResponse) 
    serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); 
    serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); 
    serverResponse.addHeader("Access-Control-Allow-Origin", "*"); 
    serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With"); 
    serverResponse.addHeader("Access-Control-Max-Age", "60"); 

    // ... 
    // ... 
} 

結論

+0

經過測試@Options,它似乎不工作,也必須在響應 – 2013-05-12 15:38:35

+0

設置標題,你可以提供更多的信息,哪些錯誤發生?因爲我在我的所有項目中都使用這個代碼。 – 2013-05-15 12:59:13

1

我覺得原來的職位將有奮力有代碼做了兩個額外的東西:

設置數據JSON.serialize(人)並設置的dataType「JSON」以來的contentType是正確的,這應該與@PUT期待使用JSON工作...

0

先試試這個 您的數據轉換爲JSON格式@ wnm3建議。

若仍面臨的問題繼續 -

幫我,如果您的請求語法是正確的。這是我做的事情,其取出415不支持的錯誤 -

@PUT 
//REMOVE THIS LINE !!!!!! ----- @Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public Response addPerson(MyJSONObj myObj) 

    // 
    //Your code here 
    return Response.ok() 
      .header("Access-Control-Allow-Origin", "*") 
      .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD") 
      .header("Access-Control-Allow-Headers", "Content-Type,Accept,X-Requested-With,authorization") 
      .header("Access-Control-Allow-Credentials", true) 
      .build(); 
} 

我正好不知道,如何讓CORS請求將發送和接受的應用程序/ JSON。 @Tobias Sarnow給出的答案部分不正確。因爲您不需要接受OPTIONS請求的方法。即使瀏覽器顯示它發送了OPTIONS請求,它仍然會使用@POST註釋查找方法。因此,我不是通過使用另一種不帶@Consumes和@Produces的方法,而是通過快速修復來實現其他方法(更精細的方法)。例 -

@PUT 
public Response addPerson() { 
    return Response.ok() 
      .header("Access-Control-Allow-Origin", "*") 
      .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD") 
      .header("Access-Control-Allow-Headers", "Content-Type,Accept,X-Requested-With,authorization") 
      .header("Access-Control-Allow-Credentials", true) 
      .build(); 
} 

@PUT 
@Consumes(MediaType.APPLICATION_JSON) 
public Response addPerson(MyJSONObj myObj) 
    // 
    //Your code here 
    // 
    return Response.ok() 
      .header("Access-Control-Allow-Origin", "*") 
      .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD") 
      .header("Access-Control-Allow-Headers", "Content-Type,Accept,X-Requested-With,authorization") 
      .header("Access-Control-Allow-Credentials", true) 
      .build(); 
} 

所以這裏發生了什麼是作爲OPTIONS初始CORS是通過第一種方法處理,那麼第二種方法處理的原始PUT請求。

我把這個答案放在這裏,因爲它花了我3-4天的時間來弄清楚爲什麼我的PUT請求沒有經過。所以它可以幫助有415個錯誤的人。

相關問題