How do I read params sent to .aspx from JavaScript with XMLHTTPRequest?

In the client side I'm doing this:

                 var this.rsSyncObject = null;
            if (window.XMLHttpRequest){
                  this.rsSyncObject = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {
                  this.rsSyncObject = new ActiveXObject('Microsoft.XMLHTTP');
            }

                 this.rsSyncObject.open("POST", "ThePage.aspx", false);      
                 this.rsSyncObject.send("param1=1;param2=2;param3=3;");
                 this.rsResult = this.rsSyncObject.responseText;

And it loads the page and waits for an answer ok, but how do I read the params I sent in the send() function from "ThePage.aspx"?
mak730Asked:
Who is Participating?
 
SThorogoodConnect With a Mentor Commented:
Actually, looking again, you have a strange format for the post data. If you use the standard URL encoding - e.g:

param1=1&param2=2 etc. (including encoding special URL chars)

then the Request.Form collection should be populated. Othrewise, you will need to read the Request.InputStream into a strign and split that manually.
0
 
SThorogoodCommented:
Use the Request.Form collection - e.g:

string param1 = Request.Form["param1"];

0
 
mak730Author Commented:
ok, Request.Form doesn't work at all, but the Request.InputStream does, in fact here is a little code to do the job:

                  private Hashtable GetHTTPParams(){
                  //Create a Stream object.
                  Stream str = Request.InputStream;
                  //Find number of bytes in stream and read.
                  int strLen = Convert.ToInt32(str.Length);                  
                  byte [] buff = new byte[strLen];
                  str.Read(buff, 0, strLen);
                  
                  //Convert byte array to a text string.
                  string s = "";
                  for(int i=0; i<strLen; i++)
                        s += Convert.ToChar(buff[i]);

                  //Convert string into a hashtable
                  Hashtable ht = new Hashtable();
                  string []data = s.Split('&');
                  s = "";
                  for(int i=0; i < data.Length; i++){
                        s = data[i];
                        if (s != ""){                              
                              string []elem = s.Split('=');
                              ht.Add(elem[0], elem[1]);
                        }
                  }
                  return ht;                  
            }
0
 
SThorogoodCommented:
Glad you got it sorted, though the Request.Form method should work. Possibly need to set the ContentType of the Request to:

application/x-www-form-urlencoded

?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.