Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15545

Re: XMLHttpRequest: POST with Data

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: XMLHttpRequest: POST with Data
Date 2012-08-24 02:55 +0000
Organization A noiseless patient Spider
Message-ID <k16qeh$mlr$1@dont-email.me> (permalink)
References <429d38h112ptste9sp22ks8sqk9cgumo7r@4ax.com>

Show all headers | View raw


On Thu, 23 Aug 2012 15:00:37 -0700, Gene Wirchenko wrote:

> Dear JavaScripters:
> 
>      I am trying to pass data back and forth using XMLHttpRequest.  I
> have run into a few issues.  The first one is the most important.
> 
>   1) I can not get POST to work according to various documentation I
> have found on the Web.
> 
>      Suppposedly, the .open() and .send() would be
>           oRequest.open("POST",Url,true); oRequest.send(DataToSend);
> but with this, at the server end, the .QueryString value is ""
> regardless of what was supposedly sent.
> 
>      I have found that using the GET style of
>           oRequest.open("POST",Url+"?"+DataToSend,true);
>           oRequest.send();
> does work.  At least, it appears to.
> 
>      Am I doing something wrong, or is the documentation wrong?
> 
>   2) What should I do for escaping of data?  Going to the server, any
> line breaks get eaten somewhere so I use escape() at browser and
> unescape() at server.  Going from server to browser, line break are not
> eaten.  Does this mean that I do not have to escape()/unescape()
> on the trip back?
> 
>      Are escape() and unescape() the correct functions to use?
> 
> Sincerely,
> 
> Gene Wirchenko

Here's some example javascript code from something I did:

Firstly the getxhr function:

// get a new XMLHttpRequest object
function getxhr() {
  if (xhrFailOnce) return null;
  var xhr;
  if (typeof XMLHttpRequest == 'undefined') {
    try { // preferred
      xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0");
    }
    catch (e1) {
      try { // best fallback
        xhr = new ActiveXObject("Msxml2.XMLHTTP.3.0");
      }
      catch (e2) {
        try { // last one to try
          xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e3) {
          xhr = false;
        }
      }
    }
  } else {
    xhr = new XMLHttpRequest();
  }
  if (!xhr) {
    if (!xhrFailOnce) {
      alert("Sorry, your browser does not support scripting features used 
on this website.");
      xhrFailOnce = true;
    }
    xhr = null;
  }
  return xhr;
}

var data = "cartid=" + cartid + "&qty=" + qty.toString();
// use an XMLHttpRequest to update the cart
var xmlhttp = getxhr();
if (!xmlhttp) return;
xmlhttp.open("POST","update_cart.php",false);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-
urlencoded");
xmlhttp.setRequestHeader("Content-length", data.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(data);

This code is for talking to php, but I don't see why any other backend 
would be different from the js viewpoint - although obviously the code in 
a VB backend will be somewhat different to that in a php backend.

But the backend should see this as the same as it would a <form 
method="post" action="blah"> </form>

Note that the data is of the format 
key1=value1&key2=value2&key3=value3.....

Note also that if your data values are taken from form fields, you will 
probably need to apply some level of validation / sanitising / escaping 
in the js, see <http://www.w3.org/TR/html401/interact/
forms.html#h-17.13.4.1>

"application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type 
must be encoded as follows:

1.    Control names and values are escaped. Space characters are replaced 
by `+', and then reserved characters are escaped as described in 
[RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', 
a percent sign and two hexadecimal digits representing the ASCII code of 
the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%
0A').

2.    The control names/values are listed in the order they appear in the 
document. The name is separated from the value by `=' and name/value 
pairs are separated from each other by `&'."

As for how the data presents itself server side, well in php it's in the 
$_POST array as $_POST["key1"], $_POST["key2"], $_POST["key3"] ...

A very quick search of google suggests that:

<%
Dim val1, val2, val3
val1 = Request.Form("key1")
val2 = Request.Form("key2")
val3 = Request.Form("key3")
%>

might be the way to read the post values in the ASP VB that handles the 
XMLHttpRequest.

Rgds

Denis McMahon

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

XMLHttpRequest: POST with Data Gene Wirchenko <genew@ocis.net> - 2012-08-23 15:00 -0700
  Re: XMLHttpRequest: POST with Data "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-08-23 22:48 +0000
    Re: XMLHttpRequest: POST with Data Gene Wirchenko <genew@ocis.net> - 2012-08-23 16:40 -0700
      Re: XMLHttpRequest: POST with Data "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-08-26 20:39 +0000
  Re: XMLHttpRequest: POST with Data Christoph Becker <cmbecker69@gmx.de> - 2012-08-24 01:11 +0200
    Re: XMLHttpRequest: POST with Data Gene Wirchenko <genew@ocis.net> - 2012-08-23 16:42 -0700
      Re: XMLHttpRequest: POST with Data Christoph Becker <cmbecker69@gmx.de> - 2012-08-24 02:54 +0200
        Re: XMLHttpRequest: POST with Data Gene Wirchenko <genew@ocis.net> - 2012-08-23 19:04 -0700
          Re: XMLHttpRequest: POST with Data Tim Streater <timstreater@greenbee.net> - 2012-08-24 08:57 +0100
    Re: XMLHttpRequest: POST with Data Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-25 12:02 +0200
      Re: XMLHttpRequest: POST with Data Christoph Becker <cmbecker69@gmx.de> - 2012-08-25 14:09 +0200
        Re: XMLHttpRequest: POST with Data Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-25 15:20 +0200
          Re: XMLHttpRequest: POST with Data Christoph Becker <cmbecker69@gmx.de> - 2012-08-26 15:49 +0200
            Re: XMLHttpRequest: POST with Data Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-26 19:15 +0200
              Re: XMLHttpRequest: POST with Data Stefan Weiss <krewecherl@gmail.com> - 2012-08-26 19:54 +0200
                Re: XMLHttpRequest: POST with Data Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-26 21:43 +0200
              Re: XMLHttpRequest: POST with Data Christoph Becker <cmbecker69@gmx.de> - 2012-08-26 23:42 +0200
  Re: XMLHttpRequest: POST with Data Denis McMahon <denismfmcmahon@gmail.com> - 2012-08-24 02:55 +0000
  Re: XMLHttpRequest: POST with Data "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2012-08-23 19:28 -0700
  Re: XMLHttpRequest: POST with Data "Tom de Neef" <tdeneef@qolor.nl> - 2012-08-24 15:58 +0200
  Re: XMLHttpRequest: POST with Data Ross McKay <au.org.zeta.at.rosko@invalid.invalid> - 2012-08-26 08:38 +1000
  Re: XMLHttpRequest: POST with Data Mike Winter <usenet@michael-winter.me.invalid> - 2012-08-26 14:59 +0100

csiph-web