请教,xmlHTTP,post数据,aspx服务端收不到数据。
脚本
sendXML();
function sendXML()
{
var xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP ");
xmlhttp.open( "POST ", "WebForm1.aspx ",false);
xmlhttp.setRequestHeader( "Content-Type ", "text/xml ");
xmlhttp.send( " <root> hello </root> ");
if (xmlhttp.status != 200)
alert(xmlhttp.statusText);
else
{
alert(xmlhttp.responseText);
}
}
服务端。
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Request.InputStream);
this.Response.Write( "OK ");
this.Response.End();
}
报的错误是Internal Server Error。
我跟踪调试时发现Request.InputStream的Length是0,也就是说看起来根本就没收到数据。
请教是哪里的问题?
------解决方案-------------------- xmlhttp.open( "POST ", "WebForm1.aspx?f= "+Math.random(),false);
每次传一个随机数过去,让页面重新加载
------解决方案--------------------把你的js写标准些,我测试是没问题的,帖出代码供参考:
a.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 无标题文档 </title>
<script language= "javascript ">
var http_request;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
http_request = new ActiveXObject( "Microsoft.XMLHTTP ");
}
else if(window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
}
if (http_request.overrideMimeType)
{
http_request.overrideMimeType( 'text/xml ');
}
}
function getResult()
{
if(http_request.readyState == 4)
{
if(http_request.status == 200)
{
alert(http_request.responseText);
}
}
}
</script>
</head>
<body>
<script language= "javascript ">
createXMLHttpRequest();
http_request.onreadystatechange = getResult;
http_request.open( 'POST ', 'b.aspx ', true);
var txt = " <test> ok </test> ";
http_request.setRequestHeader( "content-length ",txt.length);
http_request.setRequestHeader( "Content-Type ", "application/x-www-form-urlencoded ");
http_request.send(txt);
</script>
</body>
</html>
b.aspx:
protected void Page_Load(object sender, EventArgs e)
{
long i = Request.InputStream.Length;
Response.Clear();
this.Response.Write(i.ToString());
this.Response.End();
}
结果返回的长度是15