我在javascript中有以下函数调用调用php的ajax, 我想通过参数传递给javascript函数,我要访问的php页面和 所有来自html的div id都遵循我的代码:
/ ------------------------------------------- -------的JavaScript ---------------------------------- /
function getXMLHttp()
{
var xmlHttp
try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
function MakeRequest()
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("GET", "ajax.php", true);
xmlHttp.send(null);
}
function HandleResponse(response)
{
document.getElementById('ResponseDiv').innerHTML = response;
}
/ ------------------------------------------- ------- PHP ---------------------------------- /
<?php
echo "This is a php response to your request!!!!!!";
?>
/ ------------------------------------------- ------- HTML ---------------------------------- /
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type='text/javascript' src='ajax.js'></script>
<title>PHP AJAX Example</title>
</head>
<body>
<input type='button' onclick='MakeRequest();' value='Use AJAX!!!!'/>
<div id='ResponseDiv'>
This is a div to hold the response.
</div>
</body>
</html>
非常感谢你......
答案 0 :(得分:2)
别担心,如果你愿意的话,我不会告诉你你不能重新发明轮子。 ^^
function getXMLHttp()
{
var xmlHttp
try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
function MakeRequest(page, obj)
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText, obj);
}
}
xmlHttp.open("GET", page, true);
xmlHttp.send(null);
}
function HandleResponse(response, obj)
{
document.getElementById(obj).innerHTML = response;
}
这是HTML正文组件。注意我使用了“引号”而不是“撇号”。
<input type="button" onclick="MakeRequest('ajax.php', 'ResponseDiv');" value="Use AJAX!!!!"/>
<div id='ResponseDiv'>
This is a div to hold the response.
</div>