user2986042 user2986042 - 2 months ago 24
Ajax Question

Why C++ CGI form_iterator values not getting in XMLHttpRequest asynchronous Ajax request?

I wrote a sample HTML page with Ajax script . I have a .cgi file in cpp which will accept values from ajax and send message back to HTML page . Now i am facing the problem that i didn't get values in cgi script . This is my code :

HTML & ajax
script :

<html>
<head>

<script type = "text/javascript">

var XMLHttp;

if(navigator.appName == "Microsoft Internet Explorer") {

XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");

} else {
XMLHttp = new XMLHttpRequest();
}

function SentData () {

var name = document.getElementById('name').value ;
var postData;

XMLHttp.open("POST", "simplecgi.cgi", true);

postData = "";
postData += name;

XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttp.setRequestHeader("Content-length", postData.length);

XMLHttp.send(postData);

XMLHttp.onreadystatechange = function() {
document.getElementById('area').innerHTML = XMLHttp.responseText;

}

}
</script>

<h1>Simple application</h1>

<form id="newform">
Enter Name <input onkeyup = "javascript: SentData()" name ="name" id="name">
</form>

<div id = "area">
</div>

</body>
</html>


Here
SentData
function get
textarea
value and append with
postdata
and send it via
XMLHttpRequest
.

and the
cpp cgi
script :

include <unistd.h>
#include <iostream>
#include <vector>
#include <string>

#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"

#include <stdio.h>
#include <string.h>

using namespace std;
using namespace cgicc;

int main(int argc, char **argv)
{

Cgicc cgi;

try {
// Send HTTP header
cout << HTTPHTMLHeader() << endl;
// Set up the HTML document
cout << html() << head(title("cgi sample")) << endl;
cout << body() << endl;

form_iterator name = cgi.getElement("name");

if( name != cgi.getElements().end()){

cout << "Content-Type: text/plain\n\n"<< **name << "sucess"<<endl;
cout << body() << html();

}
catch(exception& e) { }

return 0 ;

}


Here i want to get HTML text area value and then i am sending that value with sucess message back to HTML .

The problem i am facing now is i didn't get that
form_iterator name = cgi.getElement("name");
value . This is empty ? Why ?

But this is working fine when i use like

<form id="newform" action="simplecgi.cgi" method="POST">
Enter Name <input onkeyup = "javascript: SentData()" name ="name" id="name">
</form>


I don;t want to redirect my HTML page to .cgi page . For that i am using ajax . Why this error ? any suggestions ?

if I didn't use
<form >
like simply a text area with out form tag . Can i pass this textarea values to .cgi and get message back to HTML ?

Answer

application/x-www-form-urlencoded content, by definition, has to be encoded using this convention.

Your javascript code appears to blindly put a bare string value in the body of the POST. That's not the application/x-www-form-urlencoded format.

The format, roughly summarizing, is a list of "name=value" pairs, separated by ampersands, with each value %-encoded like in a URL, hence "urlencoded". Your javascript code sends just the value part, and fails to %-encode it altogether.

Fix your javascript so that the POST-ed content is correctly encoded. In general, it's going to be much easier to use any one of the freely available Javascript libraries to do all the work for you, like Jquery, instead of reinventing the wheel yourself.