function callServer() ...{ // Get the city and state from the web form var city = document.getElementById("city").value; var state = document.getElementById("state").value; // Only go on if there are values for both fields if ((city ==null) || (city =="")) return; if ((state ==null) || (state =="")) return; // Build the URL to connect to var url ="/scripts/getZipCode.php?city="+ escape(city) +"&state="+ escape(state); // Open a connection to the server xmlHttp.open("GET", url, true); // Setup a function for the server to run when it's done xmlHttp.onreadystatechange = updatePage; // Send the request xmlHttp.send(null); }
处理服务器响应:
function updatePage() ...{ if (xmlHttp.readyState ==4) ...{ var response = xmlHttp.responseText; document.getElementById("zipCode").value = response; } }