<script language=javascript>
function newXMLHttpRequest()
{
var xmlreq = false;
if (window.XMLHttpRequest)
{
xmlreq = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try
{
xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e1)
{
try
{
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2)
{
alert("Create XMLHttpRequest False!");
}
}
}
return xmlreq;
}
function getstr()
{
//var xmlhttp=new XMLHttpRequest();
//var xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
var xmlhttp=newXMLHttpRequest();
xmlhttp.open("POST", "WebForm4.aspx", true);//xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlhttp.onreadystatechange=function(){
if(4==xmlhttp.readyState&&xmlhttp.status==200)
{
var state = xmlhttp.responseText;
alert(state);
}
}
xmlhttp.send();
}
</script>
function addFracasReport() { var url="controler?actionId=0_06_03_01&actionFlag=0010"; var urlmsg="&reportId="+fracasReport1.textReportId.value; //故障报告表编号 var xmlHttp=Common.createXMLHttpRequest(); xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage")); xmlHttp.open("POST",url,true); xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded); xmlHttp.send(urlmsg); }
方法 描述
abort() 停止当前请求
getAllResponseHeaders() 作为字符串返回完整的headers
getResponseHeader("headerLabel") 作为字符串返回单个的header标签
open("method","URL"[,asyncFlag[,"userName"[, "password"]]]) 设置未决的请求的目标 URL,方法,和其他参数
send(content) 发送请求
setRequestHeader("label", "value") 设置header并和请求一起发送
XMLHttpRequest对象的属性:
属性 描述
onreadystatechange 状态改变的事件触发器
readyState 对象状态(integer):
0 = 未初始化
1 = 读取中
2 = 已读取
3 = 交互中
4 = 完成
responseText 服务器进程返回数据的文本版本
responseXML 服务器进程返回数据的兼容DOM的XML文档对象
status 服务器返回的状态码, 如:404 = "文件未找到" 、200 ="成功"
statusText 服务器返回的状态文本信息
ajax页面
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="WebTestApplication1.WebForm4" %>
<%=str%>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebTestApplication1 { /// <summary> /// WebForm4 の概要の説明です。 /// </summary> public class WebForm4 : System.Web.UI.Page { public string str=string.Empty; private void Page_Load(object sender, System.EventArgs e) { // ページを初期化するユーザー コードをここに挿入します。 str="AAAAAA"; } } }
.config
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory,Ajax" />
</httpHandlers>