我有一个 AJAX 函数如下:
function ajx(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
// alert(ajaxRequest.readyState);
if(ajaxRequest.readyState == 4){
//alert(ajaxRequest.responseText);
var res = ajaxRequest.responseText;
var a = JSON.parse(res);
var v1 = a[0];
var v2 = a[1];
var v3 = a[2];
//alert(v1);
document.getElementById('vara').value = v1;
document.getElementById('varb').value = v2;
document.getElementById('varc').value = v3;
}
}
ajaxRequest.open("GET", "ajax.php", true);
ajaxRequest.send(null); }
当前所在的 HTML id
:
<div id="vara"></div>
<div id="varb"></div>
<div id="varc"></div>
而对应ajax.php
的是:
<?php
$resp = array('man','cow','dog');
echo json_encode($resp);
?>
如果我发出警报v1
v2
或v3
它分别显示man
、cow
和dog
。但它没有打印 HTML 中的值。怎么了?