I am calling a web service that inserts some data in the database using hibernate, i am just putting a button in an html page
<button type="button" onclick="testMe()">Click Me!</button>
where the testMe() method is the method that calls the database. Here is the function:
function testMe() {
$.ajax({
url: "http://localhost:8084/RESTfulExample/rest/message/insertdb",
type: "post"
});
}
and here is the web service that inserts data in the databse
@POST
@Path("/insertdb")
public void printDB() {
DB db = new DB();
db.insert();
System.out.println("Done!!!");
}
until now everything is ok and the web service inserts data in the database successfully, but what if i want to call a web service with parameters like this
@POST
@Path("/insertdb")
public void printDB(String u) {
System.out.println("inside web service");
}
and for sure modifying the html page like this
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
});
}
when i tried this and called the web service, the line that should be printed in the printDB function is not printed. What is the problem in my code when calling a web service that takes parameters?
I can see in your WebService printDB()
that you are setting @GET
http method:-
@GET
@Path("/insertdb")
public void printDB(String u) {
System.out.println("inside web service");
}
But in your $.ajax
call you are sending type: "post"
, so what you can do is try in your webservice printDB()
, change from @GET
to @POST
.
I think the problem is that your on server side you are annotating your methods with @GET
where as you are doing the Ajax
call using POST
method. The difference is that with POST
the data is sent within the request body, not as URL parameters.
So consider changing/implementing a @POST
annotated method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.