简体   繁体   English

HTTP状态415 - JQUERY中使用JERSEY实现的Restful WS中的AJAX调用不支持的媒体类型

[英]HTTP Status 415 - Unsupported Media Type for AJAX call in JQUERY to Restful WS implemented with JERSEY

Hi I am trying to post json data to Restful WS implemented with Jersey. 嗨,我正在尝试将json数据发布到使用Jersey实现的Restful WS。 I am posting data through jquery-ajax. 我通过jquery-ajax发布数据。 Why am I geting HTTP Status-415 unsupported Media type? 为什么我要使用HTTP Status-415不支持的媒体类型? Thank you. 谢谢。
Click here for screenshot of firebug description 点击此处查看firebug描述的屏幕截图

 //post method handler 
      @Path("/newentry")
        public class NewEntry {

            @POST
            @Consumes(MediaType.APPLICATION_JSON)
            public Response newEntry(String data) {
                    //doStuff
        }
    }
    // ajax call 
         $.ajax({
                 url: "http://localhost:8080/FirstRestWebService/rest/newentry",
                 type: "post",
                 data: formToJSON(),
                 dataType : "json",
                 success: function(data){
                alert("success");
                  },
               error:function(jqXHR, textStatus, errorThrown) {
                    alert("failure");
                  }   
            }); 

         function formToJSON() {
                return JSON.stringify({
                    "name": $("input#emp_name").val(),
                    ...
                    "username": $('input#username').val(),
                    "password": $('input#password').val()
                    }); 

Click here for screenshot of firebug description I was able to test the WS successfully by Jersey Client . 单击此处查看firebug说明的屏幕截图 我能够通过Jersey Client成功测试WS What is wrong in the above AJAX call? 上面的AJAX调用有什么问题? Thank you. 谢谢。

在您的AJAX调用中,您需要设置内容类型:

contentType: "application/json"

You must declare the JSON dependency. 您必须声明JSON依赖项。 Please try to add the following dependency to your pom.xml. 请尝试将以下依赖项添加到您的pom.xml中。

 <dependency>
    <groupId>com.owlike</groupId>
    <artifactId>genson</artifactId>
    <version>0.98</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM