我正在用纯 Javascript 的 AJAX 调用做一些实验,没有 JQuery。我想知道我是否可以像这样填充 DIV 标签:
<script type="text/javascript">
function call_test() {
document.getElementById("myId").innerHTML = ajax_call("example.php?id=1") ;
}
</script>
<body>
<input type="button" onClick="call_test()" value"Test">
<div id="myId">Result should be here</div>
问题是如何从 ajax_call 返回结果?我的代码如下,但不起作用:
function ajax_call(remote_file)
{
var $http,
$self = arguments.callee ;
if (window.XMLHttpRequest) {
$http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
$http = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
$http = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ($http) {
$http.onreadystatechange = function() {
if (/4|^complete$/.test($http.readyState)) {
return http.responseText ; // This only return undefined
}
};
$http.open('GET', remote_file , true);
$http.send(null);
}
}
远程文件:
<?php
echo "<h1>Jus an experiment</h1>";
?>