2016-02-24 148 views
0

如何在xmlhttprequest方法中獲取多個返回值。或建議任何Json方法檢索數據。xmlhttprequest返回多個值

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(); 
} 
+0

爲什麼不使用像jQuery或其他提供所有Ajax代碼的庫?在jQuery中,您可以使用$ .ajax()函數並將響應類型設置爲JSON。 JSON(JavaScript對象表示法)能夠用一組數據進行回覆。 –

回答

0

您可以使用jQuery並期待JSON返回值。通過使用jQuery,您也不必爲使ajax調用不同的瀏覽器方式擔憂:

$.ajax({ 
    url: "http://...", 
    dataType: 'json', 
    success: function(result, status) { 
     console.log(result); 
    } 
});