/    Sign up×
Community /Pin to ProfileBookmark

AjaxRequest Library: Capturing server response from get request

Hi:
I’m using Matt Kruse’s AjaxRequest library. I highly recommend it, but
am unexperienced with Ajax in general, and I note that his own forum
is not longer accessible.
I’m including a piece of code that I wrote using his library. It works, does
what I need it to do, but I can’t figure out how to get the responseText
member without first writing to a document hidden field and then retrieving the value.
It would be nice if I could access it directly. I.E. assign directly to a local javascript variable.
Any clues would be appreciated.
Thanks.

[code]
function UpdateHits(mlsNum){
var domain = document.domain;
var targets = new Array();
targets[“bart.johnson.com”] = ‘http://bart.johnson.com/cgi-bin/baker/AjaxUpdater.py’;
var target = targets[domain];
// Retrieve the response text via DOM from hidden input field
var response = document.forms[“getresponse”].response.value;
AjaxRequest.get({
‘url’:target, // CGI to query
// Write response text to a DOM member
‘onSuccess’:function(req){document.forms[‘getresponse’].response.value = req.responseText; },
‘onError’:function(req){ alert(“ERROR: ” + req.statusText);},
‘parameters’:{‘mls_no’:mlsNum,’ajax’:1,”test”:”from tim”},
});
alert(‘response: ‘ + response); //debug – yup, there it is, in a round-about fashion
}[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NedalsJun 07.2008 — To do what you want is pretty simple using a global variable.
<i>
</i>var response_text = '';

function UpdateHits(mlsNum){
....
AjaxRequest.get({
'url':target, // CGI to query
'onSuccess':function(req){ [b]response_text = req.responseText;[/b] },
'onError':function(req){ alert("ERROR: " + req.statusText);},
'parameters':{'mls_no':mlsNum,'ajax':1,"test":"from tim"},
});
alert('response: ' + response); //debug - yup, there it is, in a round-about fashion
}

***** BUT

That variable will NOT have any value until you get a response and you don't know when that will be.

It depends on the round trip time for the internet call.

A better method is to have onSucess call another function and do what you want there.
<i>
</i>function UpdateHits(mlsNum){
....
AjaxRequest.get({
'url':target, // CGI to query
'onSuccess':[b]AjaxReturn(req.responseText),[/b]
'onError':function(req){ alert("ERROR: " + req.statusText);},
'parameters':{'mls_no':mlsNum,'ajax':1,"test":"from tim"},
});
alert('response: ' + response); //debug - yup, there it is, in a round-about fashion
}

function AjaxReturn(response_text) {
alert('Returned:'+response_text);
// do whatever you want here
}

Copy linkTweet thisAlerts:
@tim042849authorJun 07.2008 — Hi, thanks:
'onSuccess':AjaxReturn(req.responseText), generates an "req undefined" error

Using a lambda approach:'onSuccess':function(req){AjaxReturn(req.responseText)},
executes without complaint, but the problem persists:

If I use the following code segment, I can see that the AjaxReturn function does

not execute until _after_ the UpdateHits function finishes, because the

script does not respond until after UpdateHits finishes.

? I therefore remained confused as to where to provide code

to 'capture' the response. _But_ the DOM approach gives me what I

want ...<i>
</i>function AjaxReturn(response) {
alert('response_text in function first: ' + response_text); //debug # 3
response_text = response;
alert('response_text in function last: ' + response_text); //debug #4
// do whatever you want here
}
var response_text = '****';
function UpdateHits(mlsNum){
var domain = document.domain;
var targets = new Array();
targets["bart.johnson.com"] = 'http://bart.johnson.com/cgi-bin/baker/AjaxUpdater.py';
var target = targets[domain];
// Retrieve the response text via DOM
// var response = document.forms["getresponse"].response.value;
alert('response_text coded first: ' + response_text); //debug # 1
AjaxRequest.get({
'url':target, // CGI to query
// Write response text to a DOM member
//'onSuccess':function(req){document.forms['getresponse'].response.value = req.responseText; },
'onSuccess':function(req){AjaxReturn(req.responseText)},
'onError':function(req){alert("ERROR: " + req.statusText);},
'parameters':{'mls_no':mlsNum,'ajax':1,"test":"from tim"},
});
alert('response_text coded last: ' + response_text); //debug # 2
Copy linkTweet thisAlerts:
@NedalsJun 07.2008 — ....where to provide code to 'capture' the response. [/QUOTE]
Now I'm a little confused. What do you want to do with the response?

I understood you to say that this was now working without error. I think! ?
<i>
</i>function UpdateHits(mlsNum){
...
AjaxRequest.get({
'url':target, // CGI to query // Write response text to a DOM member
'onSuccess':function(req){AjaxReturn(req.responseText)},
'onError':function(req){alert("ERROR: " + req.statusText);},
'parameters':{'mls_no':mlsNum,'ajax':1,"test":"from tim"},
});
}

function AjaxReturn(response) {
alert('response_text in function first: ' + response);
// Typically you might have an innerHTML call here to write the response to the page.
}
Copy linkTweet thisAlerts:
@tim042849authorJun 07.2008 — Now I'm a little confused. What do you want to do with the response?[/quote]
In the requested implementation, I don't need it, however I was just curious

about - if need be - how to store the request response - so the DOM method

works fine. Whether I use an InnerHTML or a form element.

I understood you to say that this was now working without error. I think!
[/quote]

Yes. Thanks!

tim
×

Success!

Help @tim042849 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...