/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Return Problem

I’m having a small issue with the following code. The text variable is coming up [I]undefined [/I]when returning the ajax [I]responseText[/I]. The [I]alert(response);[/I] gives me what is in the [I]echo.txt[/I] file but return doesn’t seem to be working.

[code=php]
var text = ajax({
‘url’ : ‘echo.txt’
});

alert(text);
[/code]

[code=php]
function ajax(params)
{
var args = { ‘method’ : ‘GET’, ‘url’ : ‘echo.txt’, ‘async’ : true }

if (params)
{
for (var index in args) { if (typeof params[index] != ‘undefined’) args[index] = params[index]; }
}

var http;
if (window.XMLHttpRequest) { http = new XMLHttpRequest(); }
else { http = new ActiveXObject(‘Microsoft.XMLHTTP’); }

http.onreadystatechange = function()
{
if (http.readyState == 4)
{
var response = http.responseText;
alert(response);
return response;
}
}

http.open(args[‘method’], args[‘url’], args[‘async’]);
http.send();
}
[/code]

Any help is appreciated.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@spengoauthorOct 21.2010 — Thanks :-) problem solved.
×

Success!

Help @spengo 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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