Two Laptop Bag*
The moose likes HTML, CSS and JavaScript and the fly likes Jquery ajax issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Jquery ajax issue" Watch "Jquery ajax issue" New topic
Author

Jquery ajax issue

RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
HI

i have javascript function in which i made ajax call with jquery. My problem is the response of ajax is not received but the next statements in javascript runs

I know about the success event of the jquery ajax. but here is the problem

on clicking the submit button


it returns me "undefined" because alert in the end is executed before the ajax response

PLEASE HELP ME!!!

Thanks
Ramandeep S

amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hi Ramandeep,
"success" is a callback function that is executed when you receive successful response for your AJAX request. I assume that you understand this as you have mentioned it in your post.
In this case Javascript will execute the "alert" statement in your code. This is normal behaviour.
May be answer to the question below will help us to resolve your issue -
Can you tell us the requirement to put that "alert" statement? I think it is just a debug mechanism that you may have implemented.

regards,
Amit
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Which alert? There are two.

And yes, code after the Ajax call will execute before the Ajax call finish. That's the asynchronous part.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
First of all


Thanks

Is there any way out.. to handle this..??

yes that was debugging alert.

Its returning me undefined

Thanks & Regards
Ramandeep SIngh
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hello,
As Bear has mentioned there are two alerts. I assumed that you are talking about second one.
Even there you are not accessing any Javascript object. I think you are getting "undefined" from some other statement probably and not the alert.

regards,
Amit
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The Ajax call is asynchronous. It goes out to the server and than the rest of your code continues on its way.

You are acting like it is synchronous.

Eric
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
Thanks to ALL for your reply

Forget about the alerts...

it was returning me undefined because ajax response takes some time

But know i used SJAX

http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX

Your Help is highly appricaited

Thanks
Ramandeep SIngh
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
RamandeepS Singh wrote:
it was returning me undefined because ajax response takes some time


It is not because it takes time to get back, it is because the call is asynchronous.



Using Synchronous calls can lead to browsers locking up.

Eric
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
Eric Pascarello wrote:
RamandeepS Singh wrote:
it was returning me undefined because ajax response takes some time


It is not because it takes time to get back, it is because the call is asynchronous.



Using Synchronous calls can lead to browsers locking up.

Eric



Eric i understand thats async.. that why next statments execute..

Ok so what should i do instead of sync request...

By Browser locking you mean for sometime?? Till the response?

Thanks & Regards
Ramandeep Singh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Why block? Use the success callback to perform any operations that need to wait for the response.
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63
Bear Bibeault wrote:Why block? Use the success callback to perform any operations that need to wait for the response.


Its returning me undefined... please look the above script i poted.. i used callback

but it was returning me as of async request

Thanks
Ramandeep S
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 514
Hello,
You may need to check if you are getting expected values for the data that is being passed to the Ajax endpoint.
As there are places where you are accessing property of an object accessed using jQuery, they can throw "undefined" in case the JS object is not present.

Regards,
amit
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Jquery ajax issue