Click to See Complete Forum and Search --> : http.responseText is null


RexChester
04-04-2009, 03:05 PM
I get an error message when trying to assign

var response = http.responseText;

with XMLHTTPREQUEST ojbect.

The correct response is output from the server, and I get a state change of
http.readyState =4 and http.status = 200, so everything goes fine excepting
the error of my var 'response not defined'.

Here is code I am using:

function createRequestObject(){
var request_o;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_o = new XMLHttpRequest();
}
return request_o;
}

var url = 'checkCaptchaContact.php';
var http = createRequestObject();

function handleCaptcha() {
if(http.readyState == 4 && http.status == 200) {
var responce = http.responseText;
document.getElementById("imlForm").innerHTML = response;
document.getElementById("captchaHdr").innerHTML = "<span>Please Re-enter the Captcha</span>";
}
}

function validateCaptcha(hdrId, formID ) {
var security = document.getElementById(formID).recaptcha_challenge_field.value;
var params = "recaptcha_challenge_field=" + security + "&recaptcha_response_field=manual_challenge" ;
http.abort;
http.open("POST",url,true);
http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = handleCaptcha;
http.send(params);
}

Any ideas greatly appreciated.

---Rex

Krik
04-04-2009, 05:18 PM
null means that nothing is being sent by "checkCaptchaContact.php"

When I run into this I go over to the PHP and run it by itself. Try to get it to output the content to the browser. Usually what you find is you have some minor error.

RexChester
04-04-2009, 10:06 PM
null means that nothing is being sent by "checkCaptchaContact.php"

When I run into this I go over to the PHP and run it by itself. Try to get it to output the content to the browser. Usually what you find is you have some minor error.

I may have misspoke. My var is 'response'. The error message Firebug gives me is:
"response is not defined" (assumed null??). I have used this simple snippet:

<?php echo "This is an XMLHTTPREQUEST test."?> in a test file

...and that does come up in browser, but it does not get assigned to http.responseText. Both the above test and the recaptcha code come up correctly in the Response from the server, but the assignment is not made to 'response'
as in the lines below:

var responce = http.responseText;
document.getElementById("imlForm").innerHTML = response;

I have put an alert statement in the "if" conditional and I do indeed reach the
inside of
if(http.readyState == 4 && http.status == 200) ,

so readyState and status should be O.K.

I was hoping I was doing something really dumb and would be caught be someone immediately, as I have never tried this XMLHttpRequest object before.

...Rex

Krik
04-04-2009, 11:18 PM
you have a typo

var responce = http.responseText;
document.getElementById("imlForm").innerHTML = response;

and undefined means you have no variable by that name.

RexChester
04-05-2009, 03:12 AM
Yes, a typo! Thanks.

Also, found this nice packaged implementation:

http://swik.net/Ajax/How+to+use+XMLHttpRequest

That does not have any typos.

---Rex