2012-07-31 2 views
0

나머지 서비스를 치는 것이 아니라, 여전히 작동하지 않는 것 같습니다 그리고 내가이 jQuery를 보내고 Status Code: HTTP/1.1 415 Unsupported Media Type내가 포럼에 비슷한 오류를 보았다

무엇입니까 :

$("#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() + "\"}"; 
}  

이는 serivce를 치는 나에게 오류를주는되지 않습니다. jQuery 쪽 (확인 된 것 같습니다) 또는 서비스받는 사람이 보낸 데이터 서식을 오류가 있는지 이해할 수 없습니다.

감사합니다. 감사합니다.

UPDATE 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

도이 방법은 NT 실제로 웹 서비스의 brakpoint을 타격 ...는 GET가 잘 작동을 수행하는 동안. – mzereba

+0

포스터 (파이어 폭스)로 테스트하면 같은 오류가 발생합니다 ... 그래서 분명히 문제는 json을 수락 할 때 Resource 클래스에 있습니다. 어느 누구도이 일을 도울 수 있습니까? – mzereba

답변

0

서비스가 예상됩니다 customer, 당신은 자바 스크립트에 개체를 만들어야합니다. 이 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

관련 문제