<html>
<head>
<script language="javascript">
function handle()
{
var arg='hello,world!';
document.write(arg);
xml=new ActiveXObject("Microsoft.xmlhttp");
xml.open("POST","123.php",false);
xml.send("arg");
window.location.href="123.php";
}
</script>
</head>
<body>
<a href="javascript:handle();">click</a>//当点击click时handle函数运行。然后用handle()把变量arg发送到服务
//器的123.php中,由123.php把arg中的字符串打印出来。
</body>
</html>
问题是,这不能实现传递。请问各位高人,怎样能够实现这种传递。不用提交form的方法。
<head>
<script language="javascript">
function handle()
{
var arg='hello,world!';
document.write(arg);
xml=new ActiveXObject("Microsoft.xmlhttp");
xml.open("POST","123.php",false);
xml.send("arg");
window.location.href="123.php";
}
</script>
</head>
<body>
<a href="javascript:handle();">click</a>//当点击click时handle函数运行。然后用handle()把变量arg发送到服务
//器的123.php中,由123.php把arg中的字符串打印出来。
</body>
</html>
问题是,这不能实现传递。请问各位高人,怎样能够实现这种传递。不用提交form的方法。
$.post("url","{'param1':'1'}",function(){});
{
if(xml.readyState==4)
{
document.myForm.time.value=xml.responseText;
}
}
xml.open("GET","123.php?timestamp="+new Date().getTime(),true);
xml.send(null);
你的代码只能运行在IE里,并且要给请求地址添加一个时间戳。
获得返回的值要用responseText。
,其次即使你把数据参数传过去了,但你使用window.location.href又去跳转页面,当然不会页面上看到值
而且你的传值也有问题 你传递的是一个“avg”的字符串,后台是无法取得的,你应该是变量=变量值,也就是xml.send("arg='hello world'");使用异步主要的好处就是提高用户的可读性,也就是局部刷新,不跳转本页面。鉴于你的需求,可以这样:1.在当前页面使用一个<div id="xhr"></div>
2.创建xhr对象xhr= null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xhr=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
if(xhr!=null)
{
xhr.open("get", "http://xxx/xxx.xx.php", false);
xhr.send("avg='hello world'"); //后台可以通过avg参数得到对应值
if (xhr.readyState==4)
{// 4 = "loaded"
if (xhr.status==200)
{// 200 = OK
content=xhr.responseText; //得到对应返回值
document.getElementById('xhr').innerText=content; //firefox 下是好像是innerContent;
}
else
{
alert("Problem retrieving XML data");
}
}
}
else
{
alert("Your browser does not support XMLHTTP.");
}