...

View Full Version : responseText returns correct value on ALERT() but...



Jawny
09-02-2010, 12:11 AM
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


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


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




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;
}

Old Pedant
09-02-2010, 12:24 AM
You just have to *declare* that variable at page scope!!!

That is:


<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.

DaveyErwin
09-02-2010, 12:25 AM
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


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


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




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; };

Old Pedant
09-02-2010, 12:28 AM
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:


try { xmlhttp = new XMLHttpRequest(); }
catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) ...

Jawny
09-02-2010, 11:20 AM
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.


<!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
echo "works";
?>

Sciliano
09-02-2010, 12:07 PM
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
$message = "First Name - " . $_POST['fname'] . "<br>";
$message .= "Surname - " . $_POST['surname'];
echo stripslashes($message);
?>

Jawny
09-02-2010, 01:47 PM
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
$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 :(

Jawny
09-02-2010, 02:25 PM
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

DaveyErwin
09-02-2010, 04:13 PM
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/

Jawny
09-02-2010, 05:47 PM
http://daveyerwin.com/

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.



<!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>

DaveyErwin
09-02-2010, 06:04 PM
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 ?

Jawny
09-02-2010, 06:05 PM
You don't have that problem with my site.
Do you ?

Works fine till i change Alert to declaring it to a variable.

DaveyErwin
09-02-2010, 06:12 PM
Works fine till i change Alert to declaring it to a variable.

Take a look now and see what you think.

Jawny
09-02-2010, 06:18 PM
Take a look now and see what you think.

Awesome :D works, pm me your PayPal :D

DaveyErwin
09-02-2010, 06:25 PM
Awesome :D works, pm me your PayPal :D

Take the kids out to the icecream vender and tell em
Uncle daves buyin.

Jawny
09-02-2010, 06:45 PM
Take the kids out to the icecream vender and tell em
Uncle daves buyin.

Ahaha thank you again Davey :)

You made my day!

DaveyErwin
09-03-2010, 12:55 AM
Ahaha thank you again Davey :)

You made my day!

Yes, your good humor has brightened mine.
Do call again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum