標籤:瀏覽器 ret java 輸出 post zhang tar 頁面 name
JS中Ajax非同步傳值
<script type="text/javasccript">
var xmlHttp;
function createXmlRequest()
{
//IE瀏覽器
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
//非IE瀏覽器
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function Startreadystate()
{
CreateXmlRequest();
xmlHttp.onreadystatechange=handlerStateChange;
xmlHttp.open("GET","Default.aspx?Name="+escape(document.getElementByIdx_x_x("txtName").value),true);
xmlHttp.send(null);
}
function handlerStateChange()
{
if(xmlHttp.readystate=="4" && xmlHttp.status=="200")
{
if(xmlHttp.responseText=="zhangsan")
{
document.getElementByIdx_x_x("label1").innerText=xmlHttp.responseText;
}
}
}
</script>
註:
GET:
//ReturnMethod為成功返回後調用的方法
xmlHttp.onreadystatechange=ReturnMethod;
xmlHttp.open("GET","Default.aspx?Name="+escape(name),true);
xmlHttp.send(null);
POST:
var para="Name="+escape(name);
xmlHttp.onreadystatechange=ReturnMethod;
xmlHttp.open("POST","Default.aspx",true);
//用POST非同步一定要加下面這個語句
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlHttp.send(para);
在Default.aspx.cs頁面中
如果用POST非同步傳值,要想在JS中用responseText接收到值,
必須用Response.Clear()先清楚
Response.Write("zhangsan");
然後用Response.End()從記憶體中輸出;
用GET非同步傳世也可不用Response.Clear();
javascript ajax請求