XMLHttpRequest:send not setting QUERY_STRING

K

keith.schincke

I know I must be missing something basic. I am developing of Firefox
1.5 and am trying to to send a basic QUERY_STRING to a test CGI that
will print the data back to the brower:

I can print my query string with an alert() right before the
request.send(queryString). An examination of the cgi enviroment and
tcpdump does not show QUERY_STRING being set.

Is there some I uncrossed or T on dotted?

Below is my javascript is mostly ripped from the Ajax Hacks book:

Thanks for any help.

Keith

var request;
var queryString = new
String("name=value&anothername=othervalue&so=on");

function sendData(){
url="http://www.exampe.edu/cgi-bin/printenv.cgi";
httpRequest("POST",url,false);
}

function httpRequest(reqType,url,asynch){
//Mozilla-based browsers
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
} else if (window.ActiveXObject){
request=new ActiveXObject("Msxml2.XMLHTTP");
if (! request){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
}
//the request could still be null if neither ActiveXObject
//initializations succeeded
if(request){
initReq(reqType,url,asynch);
} else {
alert("Your browser does not permit the use of all "+
"of this application's features!");}
}

function initReq(reqType,url,bool){
/* Specify the function that will handle the HTTP response */
request.onreadystatechange=handleJson;
request.open(reqType,url,bool);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded" ) ;
alert( "Sending: " + queryString ) ;
request.send(queryString);

if(request.readyState==4)
{
alert("Text from Server : "+request.responseText);
}
}
 
B

BinnyVA

I can print my query string with an alert() right before the
request.send(queryString). An examination of the cgi enviroment and
tcpdump does not show QUERY_STRING being set.
var request;
var queryString = new
String("name=value&anothername=othervalue&so=on");

function sendData(){
url="http://www.exampe.edu/cgi-bin/printenv.cgi";
httpRequest("POST",url,false);
}

function httpRequest(reqType,url,asynch){
//Mozilla-based browsers
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
} else if (window.ActiveXObject){
request=new ActiveXObject("Msxml2.XMLHTTP");
if (! request){
request=new ActiveXObject("Microsoft.XMLHTTP");
}
}
//the request could still be null if neither ActiveXObject
//initializations succeeded
if(request){
initReq(reqType,url,asynch);
} else {
alert("Your browser does not permit the use of all "+
"of this application's features!");}
}

function initReq(reqType,url,bool){
/* Specify the function that will handle the HTTP response */
request.onreadystatechange=handleJson;
request.open(reqType,url,bool);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded" ) ;
alert( "Sending: " + queryString ) ;
request.send(queryString);

if(request.readyState==4)
{
alert("Text from Server : "+request.responseText);
}
}

By trial and error I fixed the problem by doing this...

var queryString = "name=value&anothername=othervalue&so=on";

instead of

var queryString = new
String("name=value&anothername=othervalue&so=on");

And commenting out the following line in the initReq()

//request.onreadystatechange=handleJson;


I have no idea why this is so. I usually use an external function like
'handleJson' that will be called when the ready state changes. I had no
idea that you could get the data in the same function. Am I missing
something here?

Another alternative is you could use the GET method.

----------------
var request;
var queryString = "name=value&anothername=othervalue&so=on";

function sendData(){
url="http://www.exampe.edu/cgi-bin/printenv.cgi";
httpRequest("GET",url+"?"+queryString,false); //GET Instead of POST
- and - queryString is appended to the url
}

function httpRequest(reqType,url,asynch){
/*
...
same
...
*/
}

function initReq(reqType,url,bool){
request.open(reqType,url,bool);
alert( "Sending: " + queryString ) ;
request.send(null);//Null value is send - not the queryString
if(request.readyState==4)
{
alert("How in the world? : "+request.responseText); //Still no
idea how this works.
}
}
----------------

Please not that I have tested this script only on Firefox 1.5 on Linux
- if it breaks on others, sorry. I kinda expect it to beak on IE - but
I cannot test it - as I am on linux.

Reference
Using POST method in XMLHTTPRequest(Ajax) -
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
 
J

jake

Hey BinnyVA. Funny seeing you here. I emailed you once about your
Sudoku app which I put on my website (nonexistent). I was trying to
remember your web address just the other day, and then I saw your
username on this post. What a coincidence.

Well thats all, LOL. I never post in this group, I'm just a lurker.
 
J

jmdeschamps

BinnyVA a écrit :
By trial and error I fixed the problem by doing this...

var queryString = "name=value&anothername=othervalue&so=on";

instead of

var queryString = new
String("name=value&anothername=othervalue&so=on");

And commenting out the following line in the initReq()

//request.onreadystatechange=handleJson;


I have no idea why this is so. I usually use an external function like
'handleJson' that will be called when the ready state changes. I had no
idea that you could get the data in the same function. Am I missing
something here?

Another alternative is you could use the GET method.

----------------
var request;
var queryString = "name=value&anothername=othervalue&so=on";

function sendData(){
url="http://www.exampe.edu/cgi-bin/printenv.cgi";
httpRequest("GET",url+"?"+queryString,false); //GET Instead of POST
- and - queryString is appended to the url
}

function httpRequest(reqType,url,asynch){
/*
...
same
...
*/
}

function initReq(reqType,url,bool){
request.open(reqType,url,bool);
alert( "Sending: " + queryString ) ;
request.send(null);//Null value is send - not the queryString
if(request.readyState==4)
{
alert("How in the world? : "+request.responseText); //Still no
idea how this works.
}
}
----------------

Please not that I have tested this script only on Firefox 1.5 on Linux
- if it breaks on others, sorry. I kinda expect it to beak on IE - but
I cannot test it - as I am on linux.

Concatenating the url of the open method like so
'script.cgi?dataname=somedata', works in IE, Firefox, Opera BUT no
accented characters.

the send method work for IE, including accented characters, but not the
others...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top