• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ajax Response Text works after Two Clicks

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I just wrote a basic user-login system where the html page uses javascript to send the ajax request to a servlet which accesses through database.
Here's the js code:



But the code works only after two clicks
Any help guys?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 43 specifies that your call will be asynchronous (by specifying "true" as the last parameter):

With this in place, in your "def" function, at line 53 you will call the Ajax function, which will send a request to the server, and without waiting for a response it will then continue with the remainder of the "def" function. Since there has been no response received yet, "res" will not have the response from the server, so you are almost certainly going to go on to the else statement in line 59. It is then just a matter of luck when the response is received - I suspect that the "two clicks" is more just the amount of time it takes you to click multiple times gives the server time to return the response.

Also, please check your private messages regarding an important administrative matter.
 
Mark Wilberg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean to say that I have to change the value true to false?
Because the request may not have been processed in the servlet or is it so?
BTW, I've read from a link as follows:
Performance Note When Async is set to false, send operations are synchronous, and Windows Internet Explorer does not accept input or produce output while send operations are in progress. Therefore, this setting should not be used in situations where it is possible for a user to be waiting on the send operation to complete.

I obviously don't understand these terminologies, synchronous and asynchronous since in my case its just I have to wait for the response to complete from the server end.
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry, the terminology will get easy soon enough.

In an asynchronous request, the request / response is completely separate from how the main browser logic is working. So you can continue doing other work in the browser while waiting to get the response back (including clicking the button multiple times).

In a synchronous request, everything must happen in the order you specified, and it is only when one event has completed that the system is considered synchronized and can move on to the next task. Hence the comment that the browser can appear completely unresponsive while waiting for a response to come back.

Again, the note you mentioned is correct: generally speaking, anytime you force the user into a situation where they have no clue what is going on, and a system that appears to be dead, you risk them simply closing that system. This is a really bad user experience and should be avoided.

Normally I would recommend against using Ajax in a login screen - when you get the response back from the server (stating that the login was successful), you are presumably going to redirect to another screen. So you are effectively making the user wait for twice the length of time needed for roundtrip communications with the server.

However on the assumption that this is desirable for some reason, then I would approach it completely differently.

I would have the "def" function disable the login button until a response has been received. And I would have it always return false.

I would then have the "handleServerResponse" function check for whether the response was "y", and if it was, perform the form submission / redirection / whatever else is needed. If the response was not "y" then it would show the error message and re-enable the login button.

Just my thoughts.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Andrew. Just because you want the login to be modal, is not a good reason to make a synchronous request.

You might also want to look into using jQuery and jQuery UI which have tools that make the issuing of Ajax requests and display of mdal dialogs a lot easier.
 
Mark Wilberg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
This problem got solved.
I thought of reusing the ajaxFunction(url) but since I have to make response text a global variable, the function moves on to the next sequence only when the xmlhttp.status is changed. So, it has to wait until all other requests has been answered in the servlet.

My answer of reusing the function is a big "NO". So, i made smaller changes in the servlet and by rearranging the code by placing all the conditions to verify the response text after this:

So, after my experience of reusing this response method, the answer is pretty simple: "verify the response text asap!!!".
 
money grubbing section goes here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic