• 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

Jquery ajax issue

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

RamandeepS Singh wrote:
http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX



Using Synchronous calls can lead to browsers locking up.

Eric
 
RamandeepS Singh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

RamandeepS Singh wrote:
http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX



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
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic