Ajax传参(普通文本和XML的区别)
Ajax传参(普通文本和XML的区别)以及注意的地方时间: 2012-12-21 发送名/值对在您编写的 90% Web 应用程序中,最终都会使用名/值对发送到服务器。比方说,如果用户在网页表单中输入姓名和地址,可能希望数据采用下列形式:firstName=LarrylastName=Gullahornstreet=9018 Heatherhorn Drivecity=Rowlettstate=TexaszipCode=75080清单 1. 使用普通文本发送名/值对
function callServer() { // Get the city and state from the Web form var firstName = document.getElementById("firstName").value; var lastName = document.getElementById("lastName").value; var street = document.getElementById("street").value; var city = document.getElementById("city").value; var state = document.getElementById("state").value; var zipCode = document.getElementById("zipCode").value;
// Build the URL to connect to var url = "/scripts/saveAddress.php?firstName=" + escape(firstName) + "&lastName=" + escape(lastName) + "&street=" + escape(street) + "&city=" + escape(city) + "&state=" + escape(state) + "&zipCode=" + escape(zipCode);
// Open a connection to the server xmlHttp.open("GET", url, true);
// Set up a function for the server to run when it's done xmlHttp.onreadystatechange = confirmUpdate;
// Send the request xmlHttp.send(null);}将名/值对转化成 XML如果希望这样使用 XML 作为数据格式,首先要做的是找到一种基本 XML 格式来存储数据。显然,名/值对可以全部转化成 XML 元素,以其中的名称作为元素名,值作为元素的内容:LarryGullahorn9018 Heatherhorn DriveRowlettTexas75080清单 2. 用 XML 发送名/值对
function callServer() { // Get the city and state from the Web form var firstName = document.getElementById("firstName").value; var lastName = document.getElementById("lastName").value; var street = document.getElementById("street").value; var city = document.getElementById("city").value; var state = document.getElementById("state").value; var zipCode = document.getElementById("zipCode").value;
var xmlString = "" + " " + escape(firstName) + "" + " "