繁体   English   中英

POST REST Service不响应我的ajax jquery调用(是的,它响应)

[英]POST REST Service does not respond to my ajax jquery call (GET yes, it does)

我有一个简单的rest jerseys by jersey(java),它具有使用jquery的Web界面。 我有一个GET和POST模式的REST服务。 POST调用不起作用,它在输入数据侧具有json对象,并返回json数据。

异常消息:

A message body reader for Java class ....RESTTaskDataInput, and Java type class ....RESTTaskDataInput, and MIME media type application/x-www-form-urlencoded; charset=UTF-8 was not found.

The registered message body readers compatible with the MIME media type are:

application/x-www-form-urlencoded; charset=UTF-8 ->
  com.sun.jersey.core.impl.provider.entity.FormProvider...

帖子调用的jQuery代码(单击按钮可进行调用):

        $("#task-launcher-post").click(function()
        {                
            $("div#result").text(""); 
            $.ajax({ 
                type: "POST",                    
                url: "resources/foo", <= This is OK, the rest interface is under that
                data: {
                    code: "1234",
                    email: "my@email.com"
                },
                dataType: "json",                    
                success: function(result)
                {   
                    $("div#result").append('...');
                },
                error: function(xml,result)
                {
                    $("div#result").append('...');                    
                }
            });                                            
        });

Java REST服务代码

@Path("/foo")
public class RESTSERVICECrawler 
{
   ...

   @POST    
   @Consumes("application/json")
   @Produces("application/json")
   public RESTLaunchResult launch(RESTTaskDataInput input) 
   {                
      RESTLaunchResult result = new RESTLaunchResult();
      ...
      return result;
   }

}

Java RESTTaskDataInput代码(输入侧使用的数据)

@XmlRootElement
public class RESTTaskDataInput 
{ 
   public String code;    
   public String email;        
}

GET调用适用于url参数,但POST不适用于输入中的json。 我不知道为什么,我阅读了ajax的jquery文档,我认为rest服务器无法理解json输入数据。

尝试:

public RESTLaunchResult launch(JAXBElement<RESTTaskDataInput> input)  {
    try {
        RESTTaskDataInput data = input.getValue();
    } ...
}

解决方案感谢查理的指示。 需要设置contentType。

                var data = {
                   code: "123",
                   email: "email"
                }

                $.ajax({
                    type: "POST",                    
                    url: "resources/foo",
                    data: JSON.stringify(data),
                    dataType: "json",         
                    contentType: 'application/json',
                    success: function(result)
                    {   
                        $("div#result").append('<h5 id="result" class="text-success">' + result.success + " " + result.message + '</h5></div>');
                    },
                    error: function(xml,result)
                    {
                        $("div#result").append('<h5 id="result" class="text-error">' + result + '</h5>');     
                    }
                });

暂无
暂无

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

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