Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

passing the parameters from the ajax calling function to the 'onreadystatechange' method.

 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am making an ajax call from a javascript function and the 'onreadystatechange' method is set as

However, i want to pass one or two parameters from the original function to the callMethod function.

so, i tried changing it to

and changing the next method signature.

but it does not work as the callMethod function itself is not called.

can anyone tell is there a way to pass the parameters from the ajax calling function to the 'onreadystatechange' method.

Thanks a lot.
 
Sheriff
Posts: 67634
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

Rahul Babbar wrote:so, i tried changing it to


Do you understand why that doesn't work? You changed the function reference to a function call.

You'll need to employ closures.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I couldnt really get what you mean..
that i will need to call the function and not make a reference to it, when the ajax call returns.

Probably, it'll be great if you could lead me to an example demonstrating the same.

Thanks.
 
Bear Bibeault
Sheriff
Posts: 67634
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
Let's say that we have a function named xyz.

The code:creates a reference to the function xyz in variable a. However, the code: calls the function and assigned its return value in a.

They do something that's completely different!

When using Ajax it is really really important to understand closures. Are you familiar with the concept?
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, i am not aware of the closures concept,
i needed to use ajax in the application, and had a bit of idea about it, so i thought probably i will learn more about it as and when i progress along and encounter some issues..

Please direct me to a nice link explaining the closures..

Thanks
 
Bear Bibeault
Sheriff
Posts: 67634
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
If you use a library such as jQuery for your Ajax (highly recommended -- there are just too many nuances to keep track of to write it "by hand"), things are really easy. Consider:

How easy is that? Compare that to the number of lines of code you are now writing to make an Ajax request.

The body of the callback function has access to the variable xyz because it is part of the closure created when the function is defined inline in this way.

Essentially a closure is the combination of a function instance and all the variables that are in-scope when the function is declared. Even if the block in which the function and variables are declared goes out of scope, which would usually result in the variables being reclaimed, the variables are retained and can be used when the function executes.


 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

I am just starting to get a bit embarrassed here, because i dont really know what jquery is and how to use it and what are its advantages...
and i am inclined to use normal ajax call just to save time(i was just trying to write my own application and sort of lagging behind the timeframe which i had set.. )

Further, the problem here is this.
I am having multiple forms in the page and at one time only one is submitted and makes an ajax call, while clicking the button to make the ajax call, it passes some parameters, and one of the parameters is the method it should execute after the call returns, so my code was looking like this..


so that each form wants a different callback to execute, whose name it passes as a parameter to the function making the ajax call.
so that writing

will not work as i have to call a different method for each form.
I will probably need to avoid the generalization of the passing the method name as parameter and try to make it work.

Thanks a lot.
 
Bear Bibeault
Sheriff
Posts: 67634
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
jQuery is a really useful JavaScript library that takes a lot of work out areas that have traditionally been a royal pain in the rear when dealing with script in web pages; areas such as: DOM manipulation, event handling, animations and effects, and Ajax.

If you are going to continue to be writing JavaScript and Ajax, you owe it to yourself to check it out. I like it so much, I wrote a book about it!

With regards to your code, how is postAjax() being called? If you pass a function reference (not a function call) as the first parameter, the callback should be registered correctly.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic