By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Can it be done like this (ajax)?

P: n/a

I was playing a little bit with a lot of ajax frameworks. I have even
wrote one small by myself. But one thing still drives me crazy: Whenever
I'm doing an ajax call I need to specify handler to handle data coming
from a server script. That means that almost for every action I need two
functions.

Since I'm still new to all of this I'd like to ask how can i achieve
something like this:

function function_name(el) {
var i_need_this = get_what_i_need(el.id);
return _i_need_this;
}

where get_what_i_need function is returning some information using ajax
from a server.

Thank you

--

Ralph
Nov 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Since I'm still new to all of this I'd like to ask how can i achieve
something like this:

function function_name(el) {
var i_need_this = get_what_i_need(el.id);
return _i_need_this;
}

where get_what_i_need function is returning some information using ajax
from a server.
There are a couple of ways to do this. One is to turn of the
"asynchronous" option to XMLHttpRequest (async=false) - this will make
your script wait until the request is complete. However, most times
you don't want to do that, as server or connection problems could occur
and basically freeze your script. Also, it takes away the advantages
of async, which is that the user can go on and do something else while
waiting for the response. Instead, to use async, you have to change
your thinking from writing function-driven code to writing event-driven
code. This means code like the above isn't really the way to go.
Instead, you're going to have breaks in your code, like this:

function makeRequest(el) {
// whatever framework you're using here
// let's just say AjaxRequest(url, method, async, data, callback
new AjaxRequest(url, method, true, el.id, doSomethingWithResult);
}

function doSomethingWithResult(result) {
// takes charge after response comes back
}

If you want to preserve information across the divide you can save it
to an object:

var requests = [];

function makeRequest(el) {
...
requests.push({el: el, otherstuff:mystuff});
data = el.id + ',' + (requests.length-1)
...// make request
}

function doSomethingWithResult(result) {
// takes charge after response comes back
// your server script will have to pass back the request ID
somewhere in the result as index to requests array
}

With an average connection it will take a while, a second or so
perhaps, so you just have to design around that if you want to write
responsive Ajax apps.

-David

Nov 21 '06 #2

P: n/a
David Golightly wrote:

Thank you very much That gave me some ideas how can i solve my problem.
Thanks again David!

Ralph
>Since I'm still new to all of this I'd like to ask how can i achieve
something like this:

function function_name(el) {
var i_need_this = get_what_i_need(el.id);
return _i_need_this;
}

where get_what_i_need function is returning some information using ajax
from a server.

There are a couple of ways to do this. One is to turn of the
"asynchronous" option to XMLHttpRequest (async=false) - this will make
your script wait until the request is complete. However, most times
you don't want to do that, as server or connection problems could occur
and basically freeze your script. Also, it takes away the advantages
of async, which is that the user can go on and do something else while
waiting for the response. Instead, to use async, you have to change
your thinking from writing function-driven code to writing event-driven
code. This means code like the above isn't really the way to go.
Instead, you're going to have breaks in your code, like this:

function makeRequest(el) {
// whatever framework you're using here
// let's just say AjaxRequest(url, method, async, data, callback
new AjaxRequest(url, method, true, el.id, doSomethingWithResult);
}

function doSomethingWithResult(result) {
// takes charge after response comes back
}

If you want to preserve information across the divide you can save it
to an object:

var requests = [];

function makeRequest(el) {
...
requests.push({el: el, otherstuff:mystuff});
data = el.id + ',' + (requests.length-1)
...// make request
}

function doSomethingWithResult(result) {
// takes charge after response comes back
// your server script will have to pass back the request ID
somewhere in the result as index to requests array
}

With an average connection it will take a while, a second or so
perhaps, so you just have to design around that if you want to write
responsive Ajax apps.

-David

--

Ralph
Nov 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.