如果利用XMLHTTP.SEND提交並利用ASP在後台接收所提交資料

來源:互聯網
上載者:User
XMLHTTP.SEND(varBody)varBody:指令集。可以是XML格式資料,也可以是字串,流,或者一個不帶正負號的整數數組。也可以省略,讓指令通過Open方法的URL參數代入。 發送資料的方式分為同步和非同步兩種。在非同步方式下,資料包一旦發送完畢,就結束Send進程,客戶機執行其他的操作;而在同步方式下,客戶機要等到伺服器返回確認訊息後才結束Send進程。
方法一:varBody省略,讓指令通過Open方法的URL參數代入

 var post="id=1000&page=1";  
 var doUrl="t.asp"+post
 XMLHttp.open("POST", doUrl, false);
 XMLHttp.send(null);

 這種方法在ASP中直接利用Request.QueryString("參數名稱")
方法二:varBody為字串,Open方法定義為POST,以表單方式上傳

  var post="id=1000&page=1";  
  var doUrl="t.asp";
  XMLHttp.open("POST", doUrl, false);
  XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  XMLHttp.send(post);

 這種方法在ASP中直接利用Request.Form("參數名稱")
方法三:varBody為XML格式資料

  var xmlDom=new ActiveXObject("MSXML2.DOMDocument");
  xmlDom.loadXML("<userInfo><userName>使用者名稱稱</userName><userSex>男</userSex><userTel>028</userTel><userEmail></userEmail></userInfo>");
  XMLHttp.open("POST", "t.asp", false);
  XMLHttp.send(xmlDom.xml);

 當以這種方法傳遞時,服務端用
  <%
  Response.ContentType="text/xml"
  Response.Charset="gb2312"
  Dim xmlDom
  Set xmlDom = Server.CreateObject("Microsoft.XMLDOM")
  xmlDom.async = True
  xmlDom.load(Request)
  Response.Write xmlDom.xml
  %> 

即可,在此XM中L擷取相關內容

 

用戶端用delphi,伺服器用dot net的MVC架構擷取參數的例子:

delphi用戶端:

代碼unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComObj, StdCtrls;

type
  TForm1 = class(TForm)
    ButtonOK: TButton;
    EditURL: TEdit;
    EditParam: TEdit;
    LabelURL: TLabel;
    LabelParam: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    procedure ButtonOKClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonOKClick(Sender: TObject);
var
  url: string;
  xmlHttp, param: Olevariant;
  responseText: Widestring;
begin
  try
    url := 'http://localhost/SiteWeb/Authentication/LogOnX';
    param := 'registerNo=123456&code=Kingly&password=1';
    xmlHttp := CreateOleObject('Msxml2.XMLHTTP.3.0');
    xmlHttp.Open('POST', url, false);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');//這句很關鍵,否則伺服器檢測不到參數
    xmlHttp.send(param);
    responseText:=xmlHttp.responseText;//返回的結果
    Showmessage(responseText);
  except on E: exception do
    Showmessage(E.Message);
  end;
end;

end.

dot net伺服器寫法:

代碼[AcceptVerbs(HttpVerbs.Post)]
        [ExceptionFilter()]
        public void LogOnx()
        {
            string ErrMsg = string.Empty;
            Guid LoginId = Guid.Empty;
            Response.ContentType = "text/xml";

            if (Request.Form["registerNo"] == null)
                ErrMsg = "registerNo為空白";
            if (Request.Form["code"] == null)
                ErrMsg = "code為空白";
            if (Request.Form["password"] == null)
                ErrMsg = "password為空白";
            if (!string.IsNullOrEmpty(ErrMsg))
            {
                Response.Write(ErrMsg);
                Response.End();
            }
            string registerNo = Request.Form["registerNo"].ToString();
            string code = Request.Form["code"].ToString();
            string password = Request.Form["password"].ToString();

            if (!ValidateLogOn(registerNo, code, password))
            {
                ErrMsg = "參數為空白";
                Response.Write(ErrMsg);
                Response.End();
            }

            try
            {
                //調用伺服器登入介面,擷取LoginId的值...
            }
            catch (Exception ex) 
            {
                ErrMsg = ex.Message;
            }

            if (!string.IsNullOrEmpty(ErrMsg))
                Response.Write(ErrMsg);
            else
                Response.Write(LoginId);
            Response.End();

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.