{ "id":"1", "firstName":"vishal", "姓氏":"gehlot", "title":"先生", "officePhone":"643636", “最后修改”:”” }, { "id":"2", "firstName":"daya", "lastName":"dayaji", "title":"先生", "officePhone":"858587", “最后修改”:”” }, { "id":"7", "firstName":"tripti", “姓氏”:“三”, “标题”:“女士”, "officePhone":"4535", “最后修改”:”” }, { “id”:“59”, "firstName":"大雅", “姓氏”, “标题”:””, "officePhone":"698789", “最后修改”:”” }, { “身份证”:“89”, "firstName":"prashant", "姓氏":"p", “标题”:””, "officePhone":"987698", “最后修改”:”” }
如何使用 JavaScript 获取每个数据?
<script type="text/javascript" src="ajax.js">
function loadXMLDoc()
{
alert('inside');
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
// alert(xmlhttp.readyState);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// alert(xmlhttp.responseText);
var obj = [{"id":"1","firstName":"vishal","lastName":"gehlot","title":"Mr.","officePhone":"643636","lastModified":""},{"id":"2","firstName":"daya","lastName":"dayaji","title":"Mr.","officePhone":"858587","lastModified":""},{"id":"7","firstName":"tripti","lastName":"tri","title":"ms","officePhone":"4535","lastModified":""},{"id":"59","firstName":"Daya","lastName":"s","title":"","officePhone":"698789","lastModified":""},{"id":"89","firstName":"prashant","lastName":"p","title":"","officePhone":"987698","lastModified":""}];
alert(obj);
for(var index=0; index<obj.length;index++) {
alert((obj[index].id));
alert((obj[index].firstName));
//like this....
}
}
}
xmlhttp.open("GET","localapi.php?mode=list",true);
xmlhttp.send();
}
</script>
<input type="button" value="Check Data" onclick="loadXMLDoc()" />
这段代码对我有用,但是当我从 ajax 获取 obj 的相同响应时,它不起作用……问题是什么