Reputation: 7827
i need to call servlet post method using javascript ,i have done like this
var iframe= document.getElementById("iframe");
iframe.src = "MyServlet";
Upvotes: 2
Views: 3614
Reputation: 597016
There are two ways to POST
to a server:
<form>
with method="POST"
POST
as methodAll other options are invoking GET
That said, MyServlet
is unlikely to be a valid path. You need to specify the path that you configured as <url-pattern>
in web.xml
Upvotes: 1
Reputation: 887195
You have done it incorrectly.
You need to set the src
to a URL that will invoke the servlet, such as
iframe.src = "/Path/To/Something";
If you want to send a POST request, you'll need to create a <form action="/Path/To/Something" target="IFrameName">
and call submit()
.
Note that it is more efficient to use AJAX, with an XMLHttpRequest. The easiest way to do that is with jQuery:
$.get("/Path/To/Something");
(Although you would want to call $.post
)
Upvotes: 1