By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,744 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

XMLHttpRequest:send not setting QUERY_STRING

P: n/a
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);
}
}

Aug 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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_...using_post.php

--
Binny V A
http://binnyva.blogspot.com/

Aug 12 '06 #2

P: n/a
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.

BinnyVA wrote:
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_...using_post.php

--
Binny V A
http://binnyva.blogspot.com/
Aug 12 '06 #3

P: n/a

BinnyVA a écrit :
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.
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...
Reference
Using POST method in XMLHTTPRequest(Ajax) -
http://www.openjs.com/articles/ajax_...using_post.php

--
Binny V A
http://binnyva.blogspot.com/
Aug 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.