【问题标题】:Sending a JSON thru jQuery POST is not hitting the REST service通过 jQuery POST 发送 JSON 不会影响 REST 服务
【发布时间】:2012-07-31 12:13:33
【问题描述】:

我在论坛上查找了类似的错误,但似乎仍然无法正常工作,我得到了 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:创建了一个要传输的 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);

                } 
            });
         });

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

【问题讨论】:

  • 即使这种方式实际上并没有达到 Web 服务中的临界点......而 GET 确实可以正常工作。
  • 用Poster(Firefox)测试这个会返回同样的错误......所以显然问题出在Resource类中接受json。任何人都可以帮助解决这个问题?

标签: json rest jquery post


【解决方案1】:

您的服务需要customer,您必须在您的javascript 中创建一个对象。 这个LINK 会帮助你。

还有几点要纠正你:

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

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

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

【讨论】:

  • 感谢您的更正,第二个应该没问题,因为 JSON.stringify 解决了这个问题。我创建了一个 DTO,但仍然出现相同的错误...检查我的问题帖子中的 UPDATE 1。
猜你喜欢
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
相关资源
最近更新 更多