javascript - Sending Data from HTML to Servlet by two different methods - Stack Overflow

I've been sending data from HTML to my servlet like this :<form Action=".main.java.deri.h

I've been sending data from HTML to my servlet like this :

<form Action=".main.java.deri.hcls.caregap2.client" Method="GET">
Username: <input type="text" name="username" size="20" value="@gmail">
<BR>
<input type="submit" VALUE="submit">
<input type="reset" value="reset">
</form>

which sends the variable Username to the servlet. But I don't want to have click submit to send the data, I would like to just post the data and load the servlet without clicking anything. I've tried this :

$(document).ready(function() {
var username = "[email protected]";
$.ajax({
      type: "POST",
      url: ".main.java.deri.hcls.caregap2.client",
      data: { username: "username" }
    }).done(function( msg ) {
     // alert( "Data Saved: " + username );
      window.location = ".main.java.deri.hcls.caregap2.client?q=" + username;
    });
});

But it doesn't work, can anyone see what I'm doing wrong?? Or if I should use a different method? Help would be really appreciated!! :)

Here's my servlet method :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");{
        ServletOutputStream  out = response.getOutputStream();
        try {
            out.println("<html><head><title>" +  "</title></head>");
            out.println("<body><h1>" +  "</h1>");
            String  name = request.getParameter("username" );
            //String  ment = request.getParameter( "ment" );
            out.println("Name:" + name + "<BR>");
            //out.println("Comment: " + ment + "<BR>");
        }
        catch(Throwable  t ) {
            out.println("<P><pre>");
            t.printStackTrace( new PrintStream(out) );
            out.println ("</pre><P>");
        }
        out.println ("</body></html>");
    }

I've been sending data from HTML to my servlet like this :

<form Action="http://caregap2.appspot./src.main.java.deri.hcls.caregap2.client" Method="GET">
Username: <input type="text" name="username" size="20" value="@gmail">
<BR>
<input type="submit" VALUE="submit">
<input type="reset" value="reset">
</form>

which sends the variable Username to the servlet. But I don't want to have click submit to send the data, I would like to just post the data and load the servlet without clicking anything. I've tried this :

$(document).ready(function() {
var username = "[email protected]";
$.ajax({
      type: "POST",
      url: "http://caregap2.appspot./src.main.java.deri.hcls.caregap2.client",
      data: { username: "username" }
    }).done(function( msg ) {
     // alert( "Data Saved: " + username );
      window.location = "http://caregap2.appspot./src.main.java.deri.hcls.caregap2.client?q=" + username;
    });
});

But it doesn't work, can anyone see what I'm doing wrong?? Or if I should use a different method? Help would be really appreciated!! :)

Here's my servlet method :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");{
        ServletOutputStream  out = response.getOutputStream();
        try {
            out.println("<html><head><title>" +  "</title></head>");
            out.println("<body><h1>" +  "</h1>");
            String  name = request.getParameter("username" );
            //String  ment = request.getParameter( "ment" );
            out.println("Name:" + name + "<BR>");
            //out.println("Comment: " + ment + "<BR>");
        }
        catch(Throwable  t ) {
            out.println("<P><pre>");
            t.printStackTrace( new PrintStream(out) );
            out.println ("</pre><P>");
        }
        out.println ("</body></html>");
    }
Share Improve this question edited May 3, 2023 at 17:41 zforgo 3,3262 gold badges16 silver badges28 bronze badges asked Jul 12, 2012 at 10:18 MattTheHackMattTheHack 1,3947 gold badges31 silver badges50 bronze badges 4
  • What does it do? Have you tried alerting msg to see what it displays? – Jon Taylor Commented Jul 12, 2012 at 10:23
  • msg says "Data Saved : <html> <head> <title> </title> </head> <body> <h1> </h1> Name:username <BR> </body> </html> – MattTheHack Commented Jul 12, 2012 at 10:39
  • Also, I wouldn't use done in Ajax as you are unaware of the state. Use Success/Failure callbacks - Or at least just use "Success" on it's own and swallow failed ajax calls. – JustAnotherDeveloper Commented Jul 12, 2012 at 11:06
  • 2 @MattMcGrath Also you dont need the 'msg' in }).done(function( msg ) { if your not using MSG in the return. – JustAnotherDeveloper Commented Jul 12, 2012 at 11:14
Add a ment  | 

3 Answers 3

Reset to default 3

Your JSON data is wrong:

data: { "username": username }

First the key, than the value (variable)

Ok I think I know what it is you are tryng to do. AJAX requests are not what you want. From my understanding you are trying to load a servlet and display it without havign to interact with your page at all.

All you need to do is in javascript do the following

var username = "you username here";
window.location = "http://caregap2.appspot./src.main.java.deri.hcls.caregap2.client?username=" + username;

Using an ajax request will return the servlet body to the done method, this would be useful for displaying the information on the current page without reloading.

What you are currently doing is appending the servlet response body to the end of your query and as such redirecting to the wrong place.

Extra Info: The alternative using Ajax would be to get your servlet to return some html but not necesserily a full page, then use this response to populate part of your current page.

It seems your form is using a GET request and your ajax is performing a POST request. It is probable that your service is looking for GET parameters. Change the ajax request to use GET instead of POST

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745658576a4638692.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信