简体   繁体   English

如何在宁静的Web服务中访问Ajax JSON输入?

[英]How to access ajax json input in restful webservice?

I have an RESTful webservice that consumes a JSON response and I am making an ajax call to the web service as show below. 我有一个使用JSON响应的RESTful Web服务,并且正在对Web服务进行Ajax调用,如下所示。

When I am trying to invoke web service, I am getting 404 (Not Found) error. 当我尝试调用Web服务时,出现404 (Not Found)错误。 Please let me know what's the issue with my code. 请让我知道我的代码有什么问题。 I searched for similar posts and I couldn't find an answer. 我搜索了类似的帖子,但找不到答案。

AJAX code is: 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()
    });
}
});

And Restful web service code is: 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();
      } 

Check if there is a routing issue in your application. 检查您的应用程序中是否存在路由问题。 Try to access your REST URL from a web browser, developer console such as Firebug, or simply ping it. 尝试从Web浏览器,开发人员控制台(例如Firebug)访问REST URL,或只是对其进行ping操作。 If you still get a 404, adjust the request URL to match your web service route, or fix the routing. 如果您仍然收到404,请调整请求URL以匹配您的Web服务路由,或修复路由。

Please note that the correct naming for the content type in this case is application/json . 请注意,在这种情况下,内容类型的正确命名为application/json If this typo is also in your code, maybe start by adjusting it. 如果您的代码中也有此错别字,则可以先进行调整。

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

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