Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-02-2010, 12:11 AM   PM User | #1
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
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 
xmlhttpbComplete 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(sURLsMethodsVarsfnDone)
  {
    if (!
xmlhttp) return false;
    
bComplete false;
    
sMethod sMethod.toUpperCase();
    try {
      if (
sMethod == "GET")
      {
        
xmlhttp.open(sMethodsURL+"?"+sVarstrue);
        
sVars "";
      }
      else
      {
        
xmlhttp.open(sMethodsURLtrue);
        
xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
        
xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
      }
      
xmlhttp.onreadystatechange = function(){
        if (
xmlhttp.readyState == && !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..
Jawny is offline   Reply With Quote
Old 09-02-2010, 12:24 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-02-2010, 12:25 AM   PM User | #3
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by Jawny View Post
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 
xmlhttpbComplete 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(sURLsMethodsVarsfnDone)
  {
    if (!
xmlhttp) return false;
    
bComplete false;
    
sMethod sMethod.toUpperCase();
    try {
      if (
sMethod == "GET")
      {
        
xmlhttp.open(sMethodsURL+"?"+sVarstrue);
        
sVars "";
      }
      else
      {
        
xmlhttp.open(sMethodsURLtrue);
        
xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
        
xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
      }
      
xmlhttp.onreadystatechange = function(){
        if (
xmlhttp.readyState == && !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; };
DaveyErwin is offline   Reply With Quote
Old 09-02-2010, 12:28 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 09-02-2010, 11:20 AM   PM User | #5
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
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 
xmlhttpbComplete 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(sURLsMethodsVarsfnDone)
          {
            if (!
xmlhttp) return false;
            
bComplete false;
            
sMethod sMethod.toUpperCase();
            try {
              if (
sMethod == "GET")
              {
                
xmlhttp.open(sMethodsURL+"?"+sVarstrue);
                
sVars "";
              }
              else
              {
                
xmlhttp.open(sMethodsURLtrue);
                
xmlhttp.setRequestHeader("Method""POST "+sURL+" HTTP/1.1");
                
xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
              }
              
xmlhttp.onreadystatechange = function(){
                if (
xmlhttp.readyState == && !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..
Jawny is offline   Reply With Quote
Old 09-02-2010, 12:07 PM   PM User | #6
Sciliano
Regular Coder

 
Join Date: Nov 2009
Posts: 247
Thanks: 4
Thanked 22 Times in 22 Posts
Sciliano is an unknown quantity at this point
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);
?>
Sciliano is offline   Reply With Quote
Old 09-02-2010, 01:47 PM   PM User | #7
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
Quote:
Originally Posted by Sciliano View Post
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
Jawny is offline   Reply With Quote
Old 09-02-2010, 02:25 PM   PM User | #8
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
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
Jawny is offline   Reply With Quote
Old 09-02-2010, 04:13 PM   PM User | #9
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by Jawny View Post
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
DaveyErwin is offline   Reply With Quote
Old 09-02-2010, 05:47 PM   PM User | #10
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
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 
xmlhttpbComplete 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 (sURLsMethodsVarsfnDone) {
            if (!
xmlhttp) return false;
            
bComplete false;
            
sMethod sMethod.toUpperCase();
            try {
                if (
sMethod == "GET") {
                    
xmlhttp.open(sMethodsURL "?" sVarstrue);
                    
sVars "";
                }
                else {
                    
xmlhttp.open(sMethodsURLtrue);
                    
xmlhttp.setRequestHeader("Method""POST " sURL " HTTP/1.1");
                    
xmlhttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded");
                }
                
xmlhttp.onreadystatechange = function () {
                    if (
xmlhttp.readyState == && !bComplete) {
                        
bComplete true;
                        
fnDone(xmlhttp);
                    } 
                };
                
xmlhttp.send(sVars);
            }
            catch (
z) { return false; }
            return 
true;
        };
        return 
this;
    }  
 
 
</script> 
Jawny is offline   Reply With Quote
Old 09-02-2010, 06:04 PM   PM User | #11
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Question

Quote:
Originally Posted by Jawny View Post
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 ?
DaveyErwin is offline   Reply With Quote
Old 09-02-2010, 06:05 PM   PM User | #12
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
You don't have that problem with my site.
Do you ?
Works fine till i change Alert to declaring it to a variable.
Jawny is offline   Reply With Quote
Old 09-02-2010, 06:12 PM   PM User | #13
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by Jawny View Post
Works fine till i change Alert to declaring it to a variable.
Take a look now and see what you think.
DaveyErwin is offline   Reply With Quote
Old 09-02-2010, 06:18 PM   PM User | #14
Jawny
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Jawny is an unknown quantity at this point
Quote:
Originally Posted by DaveyErwin View Post
Take a look now and see what you think.
Awesome works, pm me your PayPal
Jawny is offline   Reply With Quote
Old 09-02-2010, 06:25 PM   PM User | #15
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Quote:
Originally Posted by Jawny View Post
Awesome works, pm me your PayPal
Take the kids out to the icecream vender and tell em
Uncle daves buyin.
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:27 AM.


Advertisement
Log in to turn off these ads.