繁体   English   中英

Web服务带有无法调用的参数

[英]web service takes parameters can not be called

我正在调用一个Web服务,该服务使用Hibernate在数据库中插入一些数据,我只是在HTML页面中放置一个按钮

<button type="button" onclick="testMe()">Click Me!</button>

其中,testMe()方法是调用数据库的方法。 这是函数:

 function testMe() {
                $.ajax({
                    url: "http://localhost:8084/RESTfulExample/rest/message/insertdb",
                    type: "post"
                });

            }

这是将数据插入数据库的Web服务

@POST
@Path("/insertdb")
public void printDB() {
        DB db = new DB();
        db.insert();
        System.out.println("Done!!!");

}

到现在为止一切正常,Web服务成功将数据插入数据库中,但是如果我想使用这样的参数调用Web服务怎么办

@POST
@Path("/insertdb")
public void printDB(String u) {
   System.out.println("inside web service");
}

并确保像这样修改html页面

    function testMe() {
            var params = {"firstName": "test", "lastName": "test2"};
            var jsonData = JSON.stringify(params);
                $.ajax({
                    url: "http://localhost:8084/RESTfulExample/rest/message/insertdb",
                    type: "post",
                    dataType: "json",
                    data: jsonData
                });

            }

当我尝试此操作并调用Web服务时,未打印应该在printDB函数中打印的行。 调用带有参数的Web服务时,我的代码有什么问题?

我可以在您的WebService printDB()看到您正在设置@GET http方法:

@GET
@Path("/insertdb")
public void printDB(String u) {
   System.out.println("inside web service");
}

但是,在你的$.ajax打电话给你要发送type: "post" ,所以你能做的就是尽量在你的web服务printDB()从变化@GET@POST

我认为问题在于,在服务器端,您正在使用@GET注释方法,而您正在使用POST方法进行Ajax调用。 区别在于使用POST ,数据是在请求正文中发送的,而不是作为URL参数发送的。

因此,请考虑更改/实现带@POST注释的方法。

暂无
暂无

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

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