Web:: XMLHTTPでPOST

ASPのページに対してXMLHTTPオブジェクトで値をPOSTするやりかたをメモ。
注意点は2点!
  1. リクエストヘッダのContent-Typeに"application/x-www-form-urlencoded"を追加!
  2. 日本語を送信する場合は、必ずescape処理を!
    ※encodeURIやencodeURIComponentなどがありますが、これだと文字化けしたためescape関数を使用しています。
★HTML側サンプル
var snddata = "hoge=" + escape("あいうえお123abc");
xmlhttp.open("POST", hoge.asp, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  xmlhttp.send(snddata);
※openの3番目の引数はtrueじゃないとダメ!

★ASP側サンプル
var rcvdata = Request.Form("hoge");
Response.ContentType = "text/html";
Response.Charset = "SHIFT-JIS";
Response.Write(rcvdata);

GETだとデータ量の上限にひっかかる場合があったので・・・
スポンサーリンク

スポンサーリンク