i didnt knwo where to put in my ajax queries so i just put it in here. hope its alright :P
i was trying a simple ajax code, where i am simply writing a text returned from a servlet to the html form.
my jsp is as below:
/********************* jsp code**********************************/
/********************* jsp code**********************************/
1. why do we have write the following in the html code:XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); i did not give this initally but then i found that at the servlet my loginid and password is coming as null
2. my servlet gets called twice. the desired result gets printed twice, and then sudden;y low and behold another time my servlet gets called, that too the doget() method. any clues.
please help i have been struggling for some time now. any help would be appreciated.
any questions on the above code would also be gladly answered
This message was edited 2 times. Last update was at by Bear Bibeault
Also, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
You can go back and change your post to add code tags by clicking the button on your post.
Stanley, are you trying to learn the inner workings of Ajax, or are you trying to get things done?
If the latter, be aware that very few experienced developers "roll their own" Ajax like this. There are just too many nuances to take care of. Rather, one of the JavaScript libraries like jQuery or Prototype will be employed.
If you're willing to go that route, I'm willing to bet that your problems will just magically go away.
i was actually trying to learn the inner workings of ajax on my own and hence wrote a very very simple ajax applications after going thru a book. but even such a simple application is not working. i mean ajax is doing its bit by submitting form without refreshing page but for some reason my servlet is getting hit twice something i simply cannot figure out.
i thought maybe going down the route of frameworks would be advisable after doing a little tinkering of my own, hence did not learn about json or prototype frameworks.
am i doing things the wrong way.
So you are saying that reducing the number of send() calls to one still invokes the servlet twice? I'm skeptical. Are you sure you are not running an old cached version of the page?
eric, please tell me how do i cancel the form submmission.
and also does this mean that when we submit a form using ajax, the form keeps on submitting unless we do something specific about it??
You return false, you really should use onsubmit on the form and not an onclick handler. You should not use javascript: in the handlers. And yes the html form has no clue about JavaScript. There is no secret handshake that says hey I am Ajax, do not submit.
Eric
Race Schedule for 2010 for this running geek:
* National Half Marathon - 03/20 [1:44:51] * Frederick Marathon - 05/02 * Baltimore Marathon - 10/16 *