.net使用Javascript XMLHttpRequest在前台调用后台方法
/*--------前台JS代码-----------*/
var http_request;
function GetAjaxObject(coaInfo)
{
if (window.XMLHttpRequest) { // Mozilla, Safari
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType)
{
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
var linkUrl = "RetentionAC.aspx?val="+coaInfo;
http_request.open("POST",linkUrl,false);
http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http_request.onreadystatechange = function(){
if(http_request.readyState == 4)
{
if(http_request.status == 200)
{
document.getElementById("ctl00_cphContent_hidCOAInfo").value = http_request.responseText;
}
}
};
http_request.send(null);
}
/*----------------------------------------------*/ /*----------后台C#代码----------------*/ if (Request.QueryString["val"] != null) { string strResult = BllCommonCostAllocation.CheckSingleCOAInfo(Request.QueryString["val"].ToString()); Response.Write(strResult); //CallBack函数中的responseText的值 Response.End();//必不可少,必须要结束,否则会返回整个HTML文件 } /*-------------------------------------*/