Ajax.Request

9 views
Skip to first unread message

kangur91

unread,
Dec 20, 2009, 6:35:02 AM12/20/09
to Prototype & script.aculo.us
My code:

function get_data_default() {
new Ajax.Request('/adress',
{
method:'post',
asynchronous:true,
onSuccess: function(data){ return data.responseText.evalJSON
(true); },
onFailure: function(){ alert('Something went wrong...') }
});
}
}
function show(){alert(get_data_default());}

I want to function get_data_default return data recived from OnSucces
function. How do that?

T.J. Crowder

unread,
Dec 20, 2009, 7:41:43 AM12/20/09
to Prototype & script.aculo.us
Hi,

> I want to function get_data_default return data recived from OnSucces
> function. How do that?

The best way is by changing your approach so that you can request the
data, and then process it later when it comes back. If you're using an
asynchronous request, there is no way for the function initiating the
request to return the result -- the result isn't available when the
function returns. You *could* use a synchronous request instead, but
that's a very bad idea -- it makes your page (at least) or the entire
browser (at worst) completely unresponsive until the request
completes, depending on what browser the user is using. Some browsers
don't even *repaint* while the request is outstanding.

(BTW: You can't return any information from the onSuccess handler;
that handler is called by Prototype, which ignores any return value
from it.)

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com

heeae

unread,
Dec 20, 2009, 10:47:15 PM12/20/09
to Prototype & script.aculo.us
I will using this approach

function get_data_default(callback){


new Ajax.Request('/adress',
{
method:'post',
asynchronous:true,

onSuccess: function(data){ callback(data.responseText.evalJSON


(true); },
onFailure: function(){ alert('Something went wrong...') }
});
}

function show(){alert(get_data_default());}

var data = null;
function processData(obj){ data = obj; alert(data.abc); }
if(data == null){
get_data_default(processData);
}else{
alert(data.abc);

joe t.

unread,
Dec 22, 2009, 12:25:33 AM12/22/09
to Prototype & script.aculo.us
> function show(){alert(get_data_default());}
This line will fail when you call show() because you're not passing a
function to fill the "callback" variable inside get_data_default().

Also, method:"post" and asynchronous:true are the defaults for those
options. You don't need to specify them if you plan to keep those
settings.
-joe t.

heeae

unread,
Dec 22, 2009, 12:59:25 AM12/22/09
to Prototype & script.aculo.us
sorry for typing mistake, I am intended to represent the concept of
using callback.
and I think keeping default variable is not bad as we cant tell the
default value not to be changed in the next generation of prototype
js.

heeae

Al C

unread,
Dec 22, 2009, 9:29:42 AM12/22/09
to Prototype & script.aculo.us
I wrestled with asynchronous calls like you have (and quite honestly,
found the structure associated with the onSuccess/onFailure lead to
confusing code)

I eventually settled on using
var myAjax = new Ajax.Updater( <target>, <url>, {method: 'post',
parameters: pars, evalScripts: true} );
(AFAIK, 'evalScripts: true' does not seem to work with Ajax.Request...
someone please corrected me if I'm wrong)

I then code within the <url> to generate the scripts that should be
spawned on success or failure (and I can set things so that the
scripts don't get called until all of the prerequisite bits are
available). Depending on the nature of the calls/results, I
occasionally set the <target> to a hidden <div>.

Once I settled on this approach, I had carefully (re-)structure my
code so that calls cascaded properly... i.e., I found that on
occasion, the same functions were getting spawned from
different<url>'s (meaning that the pages could regenerating things
unnecessarily).

Hope this helps,

Al

Alex McAuley

unread,
Dec 22, 2009, 10:21:12 AM12/22/09
to prototype-s...@googlegroups.com
If i recall correctly ...

Failure only gets called on a server error ... like a 404 ...

The failure may be due to a javascript error and would not return the alert
if that was the case.

On the back .... success only calls when the server has responded...

Ergo there are many things that can go wrong - i've listed a couple below

1. Transit. To/From the server
2. Server side scripts (will return whitespace upon an error - if error
reporting is turned off) and this is still considered a success by
javascript.

Also i noticed in the original post that you had asynchronous: true...

It is true by default so you can save a few bites there.

As TJ said in an earlier reply, You cannot return out of the Ajax request as
it is run asyncronously with the page (meaning [in easy terms] you can
execute alot of javascript at the same time as the request without locking
the browser]). To perform what you want to achieve (if i'm getting what you
want to do correctly) you should create a response handler ... something
akin to....

onSuccess : function(data) {
myHandler({data:data.responseText.evalJSON,vars:someotherVars});
}

...

function myHandler(options) {
var ReposnseData=options.data;
$('someElement').update(ResponseData);


}


Hope this helps...

Alex Mcauley
http://www.thevacancymarket.com

Hope this helps,

Al

--

You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to
prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to
prototype-scripta...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Walter Lee Davis

unread,
Dec 22, 2009, 10:54:07 AM12/22/09
to prototype-s...@googlegroups.com
This is an important thing to work on. If I know I will be using
onError on the client side, I make sure that my Ajax endpoint on the
server will return a "real" error header using PHP's header() method.

You can also get very fancy with different error header codes, too,
throwing a distinctly different error depending on the nature of that
error, and then handling it in the Ajax callback using one of the
onNNN methods instead of onError (which covers any error between 400
and 5NN, IIRC).

Walter

Alex McAuley

unread,
Dec 22, 2009, 11:38:43 AM12/22/09
to prototype-s...@googlegroups.com
An alternative i sometimes use is to set a timeout on a function to check
within a period of time - usually a hidden input with the time of the
request - checking it until its complete

That way i can know if anyting failed in the request and send it again if it
did ...

each to their own !

Alex Mcauley
http://www.thevacancymarket.com
----- Original Message -----

Reply all
Reply to author
Forward
0 new messages