XMLHttpRequest:send not setting QUERY_STRING

Discussion in 'Javascript' started by keith.schincke@gmail.com, Aug 12, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. BinnyVA Guest

    > 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

    --
    Binny V A
    http://binnyva.blogspot.com/
     
    BinnyVA, Aug 12, 2006
    #2
    1. Advertising

  3. jake Guest

    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_xmlhttp_using_post.php
    >
    > --
    > Binny V A
    > http://binnyva.blogspot.com/
     
    jake, Aug 13, 2006
    #3
  4. Guest

    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_xmlhttp_using_post.php
    >
    > --
    > Binny V A
    > http://binnyva.blogspot.com/
     
    , Aug 24, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin Crain
    Replies:
    1
    Views:
    344
    Irmen de Jong
    Jun 26, 2004
  2. benwylie
    Replies:
    4
    Views:
    382
    Ralf Stolzenberg
    Oct 19, 2006
  3. Arie Kusuma Atmaja
    Replies:
    0
    Views:
    175
    Arie Kusuma Atmaja
    Jul 21, 2006
  4. Hj Ka

    $_SERVER['QUERY_STRING']

    Hj Ka, Jul 18, 2008, in forum: Ruby
    Replies:
    1
    Views:
    167
    Matías Palomec
    Jul 18, 2008
  5. Gilbert Saint-Flour

    query_string <base> JS

    Gilbert Saint-Flour, May 18, 2004, in forum: Javascript
    Replies:
    3
    Views:
    94
    Mick White
    May 18, 2004
Loading...

Share This Page