如何在xmlhttprequest方法中获取多个返回值。 或建议任何 json 方法来检索数据。
function getcustname(supplier)
{
if (supplier.length == 0){
document.getElementById("txtcustomer").value = "";
return false;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return_str = xmlhttp.responseText;
if (return_str == "") {
document.getElementById("txtcustomer").value = "";
} else {
document.getElementById("txtcustomer").value=return_str;
}
}
}
xmlhttp.open("GET", "get_results.php?required=buyprice&p=" + supplier);
xmlhttp.send();
}
你可以使用 jQuery 并期望一个 JSON 返回值。通过使用jQuery,您也不必为进行ajax调用的不同浏览器方式而烦恼:
$.ajax({
url: "http://...",
dataType: 'json',
success: function(result, status) {
console.log(result);
}
});