Ajax
It may be that you have just met the core object of Ajax, XMLHttpRequest, to think of XML in Ajax, and perhaps you're sure it's The X in Ajax, but in fact it has nothing to do with XML directly. It has a lot to do with its name, XMLHttpRequest, and it's in Ajax. It's definitely about XML. In fact, in most AJAX applications, XML is not very much used, mainly using name/value pairs to pass the value, the following describes how to use XML format in AJAX to pass data.
First of all, from the client to the data to the server, AJAX applications are basically the text of the way to directly pass request parameters, such as:
Listing 1. Use plain text to send name/value pairs function SendMessage () {
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 url = "/scripts/saveaddress.php?firstname=" + Escape (firstName) +
&lastname= "+ Escape (lastName) +" &street= "+ escape (Street) +
" &city= "+ Escape (city) + "&state=" + Escape (state) +
"&zipcode=" + Escape (zipCode);
Xmlhttp.open ("Get", url, True);
xmlhttp.onreadystatechange = confirmupdate;
xmlhttp.send (null);
}
The example above is to simply add the parameters to the URL and then pass the data in the form of paramname1=value2¶mname2=value2.
The advantage of this is that the browser and the server only need to treat the parameters as normal text, speed faster, do not need to format conversion.
Let's look at the following examples of how AJAX applications can pass parameters to the server in XML format;
Listing 2. Sending name/value pairs in XML
Function SendMessage () {
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 ="<profile>" +
"<firstName>" + Escape (firstName) + "</firstName>" +
"<lastName>" + Escape (lastName) + "</lastName>" +
"<street>" + escape (Street) + "</street>" +
"<city>" + Escape (city) + "</city>" +
"<state>" + Escape (state) + "</state>" +
"<zip-code>" + Escape (ZipCode) + "</zip-code>" +
"</profile>";
var url = "/scripts/saveaddress.php";
Xmlhttp.open ("POST", url, True);
Xmlhttp.setrequestheader ("Content-type", "Text/xml");
Xmlhttp.onreadystatechange = confirmupdate;
Xmlhttp.send (xmlstring);
}
Now you should know what's going on, in fact, in the Ajax delivery of XML is really awkward, it requires a fixed XML format, as well as the extra characters, but also the request format to set to (Text/xml), and in the delivery of the time is not like the passing of ordinary text behind the URL passed, Instead, the Send method is delivered to the server in person, and the server also analyzes them in XML format, and then responds!! It's really waste and waste, so it's not recommended to use XML to pass data to a server unless the server accepts only XML types!
Next time we look at another situation, send an XML response message from the server!