Getting the responseText from XMLHttpRequest-Object

Posted by Sammy46 on Stack Overflow See other posts from Stack Overflow or by Sammy46
Published on 2012-10-12T09:20:02Z Indexed on 2012/10/15 15:38 UTC
Read the original article Hit count: 176

Filed under:
|
|
|
|

I wrote a cgi-script with c++ to return the query-string back to the requesting ajax object. I also write the query-string in a file in order to see if the cgi script works correctly. But when I ask in the html document for the response Text to be shown in a messagebox i get a blank message.

here is my code:

js:

<script type = "text/javascript">

var XMLHttp;
if(navigator.appName == "Microsoft Internet Explorer") {
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
XMLHttp = new XMLHttpRequest();
}

function getresponse () {
XMLHttp.open
("GET", "http://localhost/cgi-bin/AJAXTest?" + "fname=" +
document.getElementById('fname').value + "&sname=" + 
document.getElementById('sname').value,true);
XMLHttp.send(null);
}

XMLHttp.onreadystatechange=function(){
if(XMLHttp.readyState == 4)
{
document.getElementById('response_area').innerHTML +=  XMLHttp.readyState;
var x= XMLHttp.responseText
alert(x)
} 
}
</script>

First Names(s)<input onkeydown = "javascript: getresponse ()" 
id="fname" name="name"> <br>

Surname<input onkeydown = "javascript: getresponse();" id="sname">

<div id = "response_area">

</div>

C++:

int main() {

QFile log("log.txt");
if(!log.open(QIODevice::WriteOnly | QIODevice::Text))
{
    return 1;
}
QTextStream outLog(&log);
QString QUERY_STRING= getenv("QUERY_STRING");

//if(QUERY_STRING!=NULL)
//{

    cout<<"Content-type: text/plain\n\n"
        <<"The Query String is: "
        << QUERY_STRING.toStdString()<< "\n";
    outLog<<"Content-type: text/plain\n\n"
          <<"The Query String is: "
          <<QUERY_STRING<<endl;

//}

return 0;
}

I'm happy about every advice what to do!

EDIT: the output to my logfile works just fine:

Content-type: text/plain

The Query String is: fname=hello&sname=world

I just noticed that if i open it with IE8 i get the query-string. But only on the first "keydown" after that IE does nothing.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about c++