How do you get the response from a request?

598 views
Skip to first unread message

VirtuosiMedia

unread,
Sep 18, 2008, 3:42:04 PM9/18/08
to MooTools Users
Just in general, how do you access the response from the Request
object? I've been looking at the documentation and the MooTorial, but
I can't seem to make any headway. Other Ajax stuff I've done with
MooTools I haven't had to manipulate the response at all, so I've just
been able to inject it straight into the document, but now I need to
make some changes to it first. Any help would be greatly appreciated.
Thanks.

Iván N Paz

unread,
Sep 18, 2008, 4:21:29 PM9/18/08
to mootool...@googlegroups.com
Taking this code as an example:

new Request.HTML(
{
url:'some/url/script.php',
evalScripts:true,
async:false,
autoCancel:true,

onSuccess: function(html) {
//html processing here....
alert(html);
},

onFailure: function() {
alert('Error');
}
}
).get();

I would just take the html node listing, process it in whatever way I
want, then inject it....

Is that what you meant???


--
◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦
www.ivanicus.com

VirtuosiMedia

unread,
Sep 18, 2008, 4:40:33 PM9/18/08
to MooTools Users
It's close, but not quite what I meant. I'd like to be able to access
the response after the request has already been made, preferably
outside of the Request object. It's for an RSS reader, so I need to do
some parsing and Request is just being used to get the feed from a
server file. This function is a method in a class, which should return
the response in a string, but it isn't returning anything but
undefined:

fetch: function(site){
var feed;
var req = new Request({
method: this.options.method,
url: this.options.rssFetchPath,
data: { 'url' : site },
onRequest: function() {
if (this.options.targetId) { $
(this.options.targetId).setProperty('html',
this.options.onRequestMessage); }
}.bind(this),
onSuccess: function(responseText) {
feed = responseText;
}
});
req.send();
return feed;
}

On Sep 18, 1:21 pm, "Iván N Paz" <ivann...@gmail.com> wrote:
> Taking this code as an example:
>
> new Request.HTML(
> {
> url:'some/url/script.php',
> evalScripts:true,
> async:false,
> autoCancel:true,
>
> onSuccess: function(html) {
> //html processing here....
> alert(html);
> },
>
> onFailure: function() {
> alert('Error');
> }
> }
> ).get();
>
> I would just take the html node listing, process it in whatever way I
> want, then inject it....
>
> Is that what you meant???
>

Nathan White

unread,
Sep 18, 2008, 4:55:10 PM9/18/08
to mootool...@googlegroups.com
This is because your code is synchronous and the request is asynchronous. To explain your making a request it first in the background. After it fires your javascript continues executing and returns an undefined feed variable. When the onSuccess fires fetch has already processed and you have no means of accessing 'feed'.

 So you have two options

1. you can make your request synchronous by adding  async : false to your request object.
2. you can have onSucces call another function like process. something like onSuccess : this.process.bind(this).

Then in your object a simple process method.

process : function(responseText){
 
}

Personally I would avoid blocking your code using method 1. Its not a wise approach.

Method 2 will provide you will greater control and flexibility.

VirtuosiMedia

unread,
Sep 18, 2008, 5:10:54 PM9/18/08
to MooTools Users
Thanks. It took me a minute to figure out what you meant, but it's
working now.

On Sep 18, 1:55 pm, "Nathan White" <changereal...@gmail.com> wrote:
> This is because your code is synchronous and the request is asynchronous. To
> explain your making a request it first in the background. After it fires
> your javascript continues executing and returns an undefined feed variable.
> When the onSuccess fires fetch has already processed and you have no means
> of accessing 'feed'.
>
> So you have two options
>
> 1. you can make your request synchronous by adding async : false to your
> request object.
> 2. you can have onSucces call another function like process. something like
> onSuccess : this.process.bind(this).
>
> Then in your object a simple process method.
>
> process : function(responseText){
>
> }
>
> Personally I would avoid blocking your code using method 1. Its not a wise
> approach.
>
> Method 2 will provide you will greater control and flexibility.
>
Reply all
Reply to author
Forward
0 new messages