请问在客户端的ajax javascript怎样得到 服务器端的值?
以下为客户端的ff.htm ,可以把服务器端的 "www "显示出来.
<html xmlns:v= "http://www.eglic.com/ ">
<head>
<meta name= "ContentType " content= "text/html " />
<meta name= "CharSet " content= "GB2312 " />
<script type = "text/javascript " language = "javascript " >
var req; //定义变量,用来创建xmlhttprequest对象
function creatReq() // 创建xmlhttprequest,ajax开始
{
var url= "ajaxServer.aspx "; //要请求的服务端地址
if(window.XMLHttpRequest) //非IE浏览器,用xmlhttprequest对象创建
{
req=new XMLHttpRequest();
}
else if(window.ActiveXObject) //IE浏览器用activexobject对象创建
{
req=new ActiveXObject( "Microsoft.XMLHttp ");
}
if(req) //成功创建xmlhttprequest
{
req.open( "GET ",url,true); //与服务端建立连接(请求方式post或get,地址,true表示异步)
req.onreadystatechange = callback; //指定回调函数
req.send(null); //发送请求
}
}
function callback() //回调函数,对服务端的响应处理,监视response状态
{
if(req.readystate==4) //请求状态为4表示成功
{
if(req.status==200) //http状态200表示OK
{
Dispaly();
drawCanvas(); //所有状态成功,执行此函数,显示数据
}
else //http返回状态失败
{
alert( "服务端返回状态 " + req.statusText);
}
}
else //请求状态还没有成功,页面等待
{
document .getElementById ( "myTime ").innerHTML = "数据加载中 ";
}
}
function Dispaly() //接受服务端返回的数据,对其进行显示
{
document .getElementById ( "myTime ").innerHTML =req.responseText;
}
</head>
<body>
<div id= "myTime "> </div>
<input id= "Button1 " type= "button " value= "Get Time " onclick = "creatReq(); "/>
</body>
</html>
以下为服务器端的:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
Response.Write( "www ");
int t= 345;
int d= 234;
}
}
我的问题是: 怎样把服务器端的int t 和 int d传给客户端的ajax javascript语句? 程序怎么写? 先谢谢了.
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
Response.Write( "www ");
int t= 345;
int d= 234;
}
--------------------------
int t= 345;
int d= 234;
Response.Write(string.Format( "[{0},{1}] ",t,d));
Response.End();
前台:
var arr = eval(req.responseText);
alert(arr[0] + " " + arr[1]);