我是javascript和ajax世界的新手,但我正在努力学习.
现在我正在测试XMLHttpRequest,即使是最简单的例子我也无法工作.这是我正在尝试运行的代码
<script type="text/javascript">
function test() {
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200){
var container = document.getElementById('line');
container.innerHTML = xhr.responseText;
} else {
alert(xhr.status);
}
}
xhr.open('GET', 'http://www.google.com', true);
xhr.send(null);
}
</script>
Run Code Online (Sandbox Code Playgroud)
而且我总是得到状态为0的警报.我已经阅读了大量关于此的网页,我不知道我错过了什么.我将不胜感激,谢谢!