【问题标题】:How to access ajax json input in restful webservice?如何在restful webservice中访问ajax json输入?
【发布时间】:2015-05-16 19:55:30
【问题描述】:

我有一个使用 JSON 响应的 RESTful Web 服务,并且我正在对 Web 服务进行 ajax 调用,如下所示。

当我尝试调用 Web 服务时,我收到 404 (Not Found) 错误。请让我知道我的代码有什么问题。我搜索了类似的帖子,但找不到答案。

AJAX 代码是:

$('#addUser').click(function(){
var userDetails = getUserRegistrationFormInfo();
$.ajax ({
    url: 'RestfulWS/rest/restful/addUser',
    contentType: 'applicaiton/json',
    type: "POST",
    data: userDetails,
    success: function (response) {
        // Success callback
        alert('Hey');            
    }})
});


function getUserRegistrationFormInfo(){
    return JSON.stringify({
        "userId": parseInt($('#userId').val()),
        "name": $('#userName').val(),
        "addressLine": $('#address').val(),
        "emaiId": $('#email').val()
    });
}
});

而Restful web服务代码是:

  @POST
      @Path("/addUser")
      @Consumes("applicaiton/json")
      public Response addUser(User user){
          //userMap.put(Integer.toString(user.getUserId()), user);
          userMap.put("123", user);
         return Response.ok().build();
      } 

【问题讨论】:

    标签: jquery ajax json web-services rest


    【解决方案1】:

    检查您的应用程序中是否存在路由问题。尝试从 Web 浏览器、Firebug 等开发人员控制台或简单地 ping 访问您的 REST URL。如果您仍然收到 404,请调整请求 URL 以匹配您的 Web 服务路由,或修复路由。

    请注意,在这种情况下,内容类型的正确命名是 application/json。如果你的代码中也有这个错别字,不妨从调整它开始。

    【讨论】:

    • 我更正了 URL 和内容类型中的拼写错误,它起作用了。非常感谢阿德里安
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多