Enjoy an ad free experience by logging in. Not a member yet?
Register .
09-02-2010, 12:11 AM
PM User |
#1
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
responseText returns correct value on ALERT() but...
RESOLVED
Thank you!
Greetings all!
Im looking for some help regarding responseText, so please have a look
im so mad right now because i have been struggeling for hours
XMLHttpRequestObject.responseText returns correct value when i do
alert(XMLHttpRequestObject.responseText); see line
PHP Code:
var fnWhenDone = function ( XMLHttpRequestObject ) { alert ( XMLHttpRequestObject . responseText ); };
But problem is that i want to save down the response to a variable... so i try to change it into
PHP Code:
var fnWhenDone = function ( XMLHttpRequestObject ) { varTest = XMLHttpRequestObject . responseText ; };
When i try to alert varTest later i get "Undifined"... im pretty new to javascript and have been stuck for hours ...
See full code below
PHP Code:
var myConn = new XHConn ();
if (! myConn ) { alert ( "XMLHTTP not available. Try a newer/better browser." ); }
var fnWhenDone = function ( XMLHttpRequestObject ) { alert ( XMLHttpRequestObject . responseText ); };
myConn . connect ( "validateSearch.php" , "POST" , "foo=bar&baz=qux" , fnWhenDone );
function XHConn ()
{
var xmlhttp , bComplete = false ;
try { xmlhttp = new ActiveXObject ( "Msxml2.XMLHTTP" ); }
catch ( e ) { try { xmlhttp = new ActiveXObject ( "Microsoft.XMLHTTP" ); }
catch ( e ) { try { xmlhttp = new XMLHttpRequest (); }
catch ( e ) { xmlhttp = false ; }}}
if (! xmlhttp ) return null ;
this . connect = function( sURL , sMethod , sVars , fnDone )
{
if (! xmlhttp ) return false ;
bComplete = false ;
sMethod = sMethod . toUpperCase ();
try {
if ( sMethod == "GET" )
{
xmlhttp . open ( sMethod , sURL + "?" + sVars , true );
sVars = "" ;
}
else
{
xmlhttp . open ( sMethod , sURL , true );
xmlhttp . setRequestHeader ( "Method" , "POST " + sURL + " HTTP/1.1" );
xmlhttp . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" );
}
xmlhttp . onreadystatechange = function(){
if ( xmlhttp . readyState == 4 && ! bComplete )
{
bComplete = true ;
fnDone ( xmlhttp );
}};
xmlhttp . send ( sVars );
}
catch( z ) { return false ; }
return true ;
};
return this ;
}
Last edited by Jawny; 09-02-2010 at 06:19 PM ..
09-02-2010, 12:24 AM
PM User |
#2
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
You just have to *declare* that variable at page scope!!!
That is:
Code:
<script>
var varTest;
...
... later in the code ...
var fnWhenDone = function (XMLHttpRequestObject) {
varTest = XMLHttpRequestObject.responseText; };
...
</script>
Or some equivalent way. The point is that varTest has to exist outside of the fnWhenDone function if you then want to *see* it outside the function.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
09-02-2010, 12:25 AM
PM User |
#3
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
Quote:
Originally Posted by
Jawny
Greetings all!
Im looking for some help regarding responseText, so please have a look
im so mad right now because i have been struggeling for hours
XMLHttpRequestObject.responseText returns correct value when i do
alert(XMLHttpRequestObject.responseText); see line
PHP Code:
var fnWhenDone = function ( XMLHttpRequestObject ) { alert ( XMLHttpRequestObject . responseText ); };
But problem is that i want to save down the response to a variable... so i try to change it into
PHP Code:
var fnWhenDone = function ( XMLHttpRequestObject ) { varTest = XMLHttpRequestObject . responseText ; };
When i try to alert varTest later i get "Undifined"... im pretty new to javascript and have been stuck for hours ...
See full code below
PHP Code:
var myConn = new XHConn ();
if (! myConn ) { alert ( "XMLHTTP not available. Try a newer/better browser." ); }
var fnWhenDone = function ( XMLHttpRequestObject ) { alert ( XMLHttpRequestObject . responseText ); };
myConn . connect ( "validateSearch.php" , "POST" , "foo=bar&baz=qux" , fnWhenDone );
function XHConn ()
{
var xmlhttp , bComplete = false ;
try { xmlhttp = new ActiveXObject ( "Msxml2.XMLHTTP" ); }
catch ( e ) { try { xmlhttp = new ActiveXObject ( "Microsoft.XMLHTTP" ); }
catch ( e ) { try { xmlhttp = new XMLHttpRequest (); }
catch ( e ) { xmlhttp = false ; }}}
if (! xmlhttp ) return null ;
this . connect = function( sURL , sMethod , sVars , fnDone )
{
if (! xmlhttp ) return false ;
bComplete = false ;
sMethod = sMethod . toUpperCase ();
try {
if ( sMethod == "GET" )
{
xmlhttp . open ( sMethod , sURL + "?" + sVars , true );
sVars = "" ;
}
else
{
xmlhttp . open ( sMethod , sURL , true );
xmlhttp . setRequestHeader ( "Method" , "POST " + sURL + " HTTP/1.1" );
xmlhttp . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" );
}
xmlhttp . onreadystatechange = function(){
if ( xmlhttp . readyState == 4 && ! bComplete )
{
bComplete = true ;
fnDone ( xmlhttp );
}};
xmlhttp . send ( sVars );
}
catch( z ) { return false ; }
return true ;
};
return this ;
}
var fnWhenDone = function (XMLHttpRequestObject......
fnDone(xmlhttp);
Is this a typo or what ?
anyways...
fnDone(xmlhttp.responseText);
var fnWhenDone = function (text) { varTest = text; };
09-02-2010, 12:28 AM
PM User |
#4
Supreme Master coder!
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
I would suggest that you are trying to find the xmlhttp object in the wrong order. Current versions of MSIE support the standard, so go for it first:
Code:
try { xmlhttp = new XMLHttpRequest(); }
catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) ...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
09-02-2010, 11:20 AM
PM User |
#5
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hey guys thank you for trying to help me! i really appreciate it!
I took your advice i tryed but still not luck
i think its really odd and still getting undifined.
I got it to work if i declared the variable
validateResponse outside the function
function get(obj) but but then i ended up getting undifined in the first click but correct second click and next.
PHP Code:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
< html >
< head >
< meta http - equiv = "Content-Type" content = "text/html; charset=iso-8859-1" />
< meta http - equiv = "Content-Language" content = "sv" >
< title > test </ title >
</ head >
< body >
< script type = "text/javascript" >
function get ( obj )
{
var validateResponse ;
var myConn = new XHConn ();
if (! myConn ) { alert ( "XMLHTTP not available. Try a newer/better browser." ); }
var fnWhenDone = function ( XMLHttpRequestObject ) { validateResponse = XMLHttpRequestObject . responseText ; };
myConn . connect ( "validateSearch.php" , "POST" , "foo=bar&baz=qux" , fnWhenDone );
alert ( validateResponse );
}
function XHConn ()
{
var xmlhttp , bComplete = false ;
try { xmlhttp = new XMLHttpRequest (); }
catch ( e ) { try { xmlhttp = new ActiveXObject ( "Msxml2.XMLHTTP" ); }
catch ( e ) { try { xmlhttp = new ActiveXObject ( "Microsoft.XMLHTTP" ); }
catch ( e ) { xmlhttp = false ; }}}
if (! xmlhttp ) return null ;
this . connect = function( sURL , sMethod , sVars , fnDone )
{
if (! xmlhttp ) return false ;
bComplete = false ;
sMethod = sMethod . toUpperCase ();
try {
if ( sMethod == "GET" )
{
xmlhttp . open ( sMethod , sURL + "?" + sVars , true );
sVars = "" ;
}
else
{
xmlhttp . open ( sMethod , sURL , true );
xmlhttp . setRequestHeader ( "Method" , "POST " + sURL + " HTTP/1.1" );
xmlhttp . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" );
}
xmlhttp . onreadystatechange = function(){
if ( xmlhttp . readyState == 4 && ! bComplete )
{
bComplete = true ;
fnDone ( xmlhttp );
}};
xmlhttp . send ( sVars );
}
catch( z ) { return false ; }
return true ;
};
return this ;
}
</script>
<form action="javascript :get(document.getElementById('myform'));" method="post" autocomplete="off" name="myform" id="myform">
<input type="submit" value="Search" name="submit">
</form>
</body>
</html>
validateSearch.php
PHP Code:
<?php
echo "works" ;
?>
Last edited by Jawny; 09-02-2010 at 11:23 AM ..
09-02-2010, 12:07 PM
PM User |
#6
Regular Coder
Join Date: Nov 2009
Posts: 247
Thanks: 4
Thanked 22 Times in 22 Posts
Jawny:
Here's a live example of AJAX Post, with responseText:
http://www.javascript-demos.com/1/AJAX_Post.html
This is the .php file associated with that demo:
PHP Code:
<?php
$message = "First Name - " . $_POST [ 'fname' ] . "<br>" ;
$message .= "Surname - " . $_POST [ 'surname' ];
echo stripslashes ( $message );
?>
09-02-2010, 01:47 PM
PM User |
#7
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
Sciliano
Jawny:
Here's a live example of AJAX Post, with responseText:
http://www.javascript-demos.com/1/AJAX_Post.html
This is the .php file associated with that demo:
PHP Code:
<?php
$message = "First Name - " . $_POST [ 'fname' ] . "<br>" ;
$message .= "Surname - " . $_POST [ 'surname' ];
echo stripslashes ( $message );
?>
I had a look on that code and i still dont understand why mine doesnt work
09-02-2010, 02:25 PM
PM User |
#8
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Okay i will pay $20 (PayPal) to the one who fixes my problems and posts it first.
My work has stalled and i really need to keep going
Best Regards
Jawn
09-02-2010, 04:13 PM
PM User |
#9
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
Quote:
Originally Posted by
Jawny
Okay i will pay $20 (PayPal) to the one who fixes my problems and posts it first.
My work has stalled and i really need to keep going
Best Regards
Jawn
http://javascript.daveyerwin.com/
Last edited by DaveyErwin; 09-03-2010 at 10:49 AM ..
Reason: url change
09-02-2010, 05:47 PM
PM User |
#10
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
DaveyErwin
Took your code and changed it into how i wanted but problem persists...
First click i get "undifined" second and next one send me the correct value.
PHP Code:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< title ></ title >
</ head >
< body >
Hiyas , click the button !
< input type = "button" onclick = "doit()" />
</ body >
</ html >
< script >
var myConn = new XHConn ();
var response ;
if (! myConn ) { alert ( "XMLHTTP not available. Try a newer/better browser." ); }
var fnWhenDone = function ( XMLHttpRequestObject ) { response = XMLHttpRequestObject . responseText ; };
doit = function () {
myConn . connect ( "validateSearch.php" , "GET" , "foo=bar&baz=qux" , fnWhenDone );
if( response == "works" )
{
alert ( 'Searching...' );
}
else
{
// Show error
alert ( response );
}
}
function XHConn () {
var xmlhttp , bComplete = false ;
try { xmlhttp = new ActiveXObject ( "Msxml2.XMLHTTP" ); }
catch ( e ) {
try { xmlhttp = new ActiveXObject ( "Microsoft.XMLHTTP" ); }
catch ( e ) {
try { xmlhttp = new XMLHttpRequest (); }
catch ( e ) { xmlhttp = false ; }
}
}
if (! xmlhttp ) return null ;
this . connect = function ( sURL , sMethod , sVars , fnDone ) {
if (! xmlhttp ) return false ;
bComplete = false ;
sMethod = sMethod . toUpperCase ();
try {
if ( sMethod == "GET" ) {
xmlhttp . open ( sMethod , sURL + "?" + sVars , true );
sVars = "" ;
}
else {
xmlhttp . open ( sMethod , sURL , true );
xmlhttp . setRequestHeader ( "Method" , "POST " + sURL + " HTTP/1.1" );
xmlhttp . setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" );
}
xmlhttp . onreadystatechange = function () {
if ( xmlhttp . readyState == 4 && ! bComplete ) {
bComplete = true ;
fnDone ( xmlhttp );
}
};
xmlhttp . send ( sVars );
}
catch ( z ) { return false ; }
return true ;
};
return this ;
}
</script>
09-02-2010, 06:04 PM
PM User |
#11
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
Quote:
Originally Posted by
Jawny
Took your code and changed it into how i wanted but problem persists...
First click i get "undifined" second and next one send me the correct value.
You don't have that problem with my site.
Do you ?
09-02-2010, 06:05 PM
PM User |
#12
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
DaveyErwin
You don't have that problem with my site.
Do you ?
Works fine till i change Alert to declaring it to a variable.
09-02-2010, 06:12 PM
PM User |
#13
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
Quote:
Originally Posted by
Jawny
Works fine till i change Alert to declaring it to a variable.
Take a look now and see what you think.
09-02-2010, 06:18 PM
PM User |
#14
New to the CF scene
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
DaveyErwin
Take a look now and see what you think.
Awesome
works, pm me your PayPal
09-02-2010, 06:25 PM
PM User |
#15
Regular Coder
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
Quote:
Originally Posted by
Jawny
Awesome
works, pm me your PayPal
Take the kids out to the icecream vender and tell em
Uncle daves buyin.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:27 AM .
Advertisement
Log in to turn off these ads.