通过jQuery POST发送JSON不会影响REST服务

时间:2012-07-31 12:13:34

标签: json rest jquery post

我在论坛上查了类似的错误,但似乎没有工作,我得到了  Status Code: HTTP/1.1 415 Unsupported Media Type

我发送这个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() + "\"}";
}      

它没有击中服务并给我错误。我无法理解错误是在格式化jQuery端的发送数据(似乎没问题)或服务接收器中。

感谢任何帮助,谢谢!

更新1:创建转移目的地

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

                } 
            });
         });

但我仍然得到同样的错误!

1 个答案:

答案 0 :(得分: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"}';