2012-07-31 53 views
0

我擡起頭,在論壇上類似的錯誤,但似乎無法正常工作還是和我得到 Status Code: HTTP/1.1 415 Unsupported Media Type發送JSON通jQuery的POST不打REST服務

我送這個jQuery:

$("#btnInsertCustomer").click(function(){ 
      var myObj = {name: "Bill Adama", address:"Vancouver Canada"}; 
      var jsondata = JSON.stringify(myObj); 

      $.ajax({ 
       type: "POST", 
       url: "http://localhost:8081/RestDemo/services/customers/add", 
       contentType: "application/json", 
       dataType: "json", 
       data: jsondata, 
       success: function (resp, status, xhr) { 
        var msg = "Name: " + resp.name + ", Address: " + resp.address; 
        alert(msg); 
        $("#successPost").html(msg + " - STATUS: " + xhr.status + " " + xhr.statusText); 

       }, 
       error: function(resp, status, xhr){ 
        alert("Error: " + resp.e); 
        $("#errorPost").html("Error: " + resp.e + " - STATUS: " + xhr.status + " " + xhr.statusText); 

       } 
      }); 
     }); 

此以下資源:

@POST 
    @Path("/add") 
    @Produces("application/json") 
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 
    public String addCustomer(Customer customer) { 
     //insert 
     int id = customerMap.size(); 
     customer.setId(id); 
     customerMap.put(id, customer); 
     //get inserted 
     Customer result = customerMap.get(id); 

     return "{\"name\": \" " + result.getName() + " \", \"address\": \"" + result.getAddress() + "\"}"; 
}  

它沒有擊中中的service,並給我的錯誤。我無法理解,如果錯誤是格式化發送數據在jQuery端(這似乎是好的)或在服務接收器。

任何幫助表示讚賞,謝謝!

更新1:創建一個DTO移轉給

$("#btnInsertCustomer").click(function(){ 
      //var myObj = '{"name": "Bill Adama", "address":"Vancouver Canada"}'; 
      //var jsondata = JSON.stringify(myObj); 

      var NewCustomer = { }; 
      NewCustomer.name = "Bill Adama"; 
      NewCustomer.address = "Vancouver Canada"; 
      var DTO = { 'NewCustomer' : NewCustomer }; 

      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "http://localhost:8081/RestDemo/services/customers/add", 
       data: JSON.stringify(DTO), 
       dataType: "json", 

       success: function (resp, status, xhr) { 
        var msg = "Name: " + resp.name + ", Address: " + resp.address; 
        alert(msg); 
        $("#successPost").html(msg + " - STATUS: " + xhr.status + " " + xhr.statusText); 

       }, 
       error: function(resp, status, xhr){ 
        alert("Error: " + resp.e); 
        $("#errorPost").html(resp.e + " - STATUS: " + xhr.status + " " + xhr.statusText); 

       } 
      }); 
     }); 

,但我仍然得到同樣的錯誤!

+0

即使這種方式實際上新臺幣創下了Web服務的brakpoint ......而搞定不做工精細。 – mzereba 2012-08-01 11:05:11

+0

用海報(Firefox)測試這個結果給出了同樣的錯誤...顯然問題出在Resource類中接受json。任何人都可以幫忙嗎? – mzereba 2012-08-01 12:02:57

回答

0

你的服務期待customer,你必須在你的javascript中創建一個對象。 這個LINK會幫助你。

而且幾點糾正你:

contentType: "application/json; charset=utf-8", 

This >> myObj = {name: "Bill Adama", address:"Vancouver Canada"}; 

myObj = '{"name": "Bill Adama", "address" :"Vancouver Canada"}'; 
+0

感謝您的更正,第二個應該很好,因爲JSON.stringify修復了這個問題。我創建了一個DTO,但仍然得到相同的錯誤...在我的問題的帖子中檢查UPDATE 1。 – mzereba 2012-07-31 13:03:46