Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote call returning undefined. 1

Status
Not open for further replies.

travisbrown

Technical User
Dec 31, 2001
1,016
I'm trying to return a string value from a remote call to a variable. I'm getting 'undefined' and I'm not sure why. Any ideas? The remote page is a simple text string that returns correctly when called up directly.

Code:
<script type="text/javascript">
function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function getString(page) {
    var http = createRequestObject();
    http.open( 'get', page , true);
	http.onreadystatechange = function(){
         if ( http.readyState == 4 ) {
        var response = responseText;
        return response;
        http = null;
        }    
    };
    http.send(null);
}

function test() {
alert(getString('test.asp'));
};
</script>
<a href="javascript:test();">test</a>
 
Try this:

Code:
if (http.readyState == 4 && http.status == 200) {
   var response = [!]http[/!].responseText;

I'm also wary about using http as a variable name.

[monkey][snake] <.
 
Humm...no. I get the same thing - undefined

Good point on the var name.

Code:
<script type="text/javascript">
function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function getString(page) {
    var remote = createRequestObject();
    remote.open( 'get', page , true);
	remote.onreadystatechange = function(){
	if (remote.readyState == 4 && remote.status == 200) {
   		var response = remote.responseText;
        return response;
        remote = null;
        }    
    };
    remote.send(null);
}

function test() {
alert(getString('test.asp'));
};
</script>
<a href="javascript:test();">test</a>

I use this, which works quite well. I was trying to adapt the script above based on this.

Code:
function sndReq(page,target) {
    var http = createRequestObject();
    http.open( 'get', page , true);
    http.onreadystatechange = function(){
         if ( http.readyState == 4 ) {
        var response = http.responseText;
        document.getElementById(target).innerHTML = response;
        http=null;
        }    
    };
    http.send(null);
}
 
Here, I came up with something:

Code:
<script type="text/javascript">
[!]var response = new String();[/!]
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

function getString(page) {
    var http = createRequestObject();
    http.open( 'get', page , true);
    http.onreadystatechange = function() {
         if ( http.readyState == 4 ) {
            [!]response = http.responseText;[/!]
            http = null;
        }    
    };
    http.send(null);
    [!]return response;[/!]
}

function test() {
alert(getString('test2.asp'));
};
</script>
<a href="javascript:test();">test</a>

Changes highlighted. I tested this and it worked.


[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top