Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jquery ajax issue

 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which alert? There are two.

And yes, code after the Ajax call will execute before the Ajax call finish. That's the asynchronous part.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why block? Use the success callback to perform any operations that need to wait for the response.
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic