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客户端:
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服务器写法:
[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();