Guide: The article describes how to communicate with the server side with XMLHTTP and introduce some specific applications.
XML introduction 28: XMLHTTP: Website Super binder
Translation: Batman
Introduction
Many ASP developers want to use Microsoft to support XML in their own website.
New features. Among them, some people find that I can use XML to decorate the website, but if only
If you use XMLDOM, you will lose some more important things. After all, XML is used
As an online data performance and the image exchange of data exchange. Although it can be very satisfied with XML
Draw your data, but developers have to use CGI to make browser and servers.
Data exchange unless you use an XML document using the browser and the client.
Of course, the CGI can be completely competent from the perspective of conveying information, but if if it is to come and XML
Let XML lose a lot of own use. Fortunately, Microsoft provides a more
Effective methods to transmit XML, although this method is largely not paid attention to.
There is a series of objects in the MSXML interpreter package provided by Microsoft, maybe no one will pay attention to it.
XMLHTTPCONNECTION object. In short, it allows you to open a HTTP connection on the server.
Send some data and retrieve some data. And all this can be implemented in a few pieces of scripts.
Using XMLHTTP objects is usually XML data exchange, but other formatted data is also allowed.
Application in business procedures
This type of exchange type is a text string to the server that the client sends an XML format to the server.
The server then loads this string into an XMLDOM object and explains it, then return a paragraph.
HTML gives the client, or another XML code gives the client's browser to explain itself.
In this way, it is very effective for the delivery of information, especially when you are using
DHTML allows you to dynamically display according to the return information.
Examples are as follows (only IE5 is installed in the client and the server)
<%
IF (Request.serverVariables ("request_method") == "post")
{
Var Req = Server.createObject ("Microsoft.xmLdom");
Req.async = false;
Req.Load (Request);
IF (Req.Documentelement.nodeName == "TimeSheet")
{
// Some processing is performed casually. . .
.....
Response.write ("
}
}
Else
{%>
Function senddata () {
VAR XMLHTTP = New ActiveXObject ("Microsoft.xmlhttp");
XMLHTTP.Open ("post", "http://www.yoursite.com/thispage.asp", false); xmlhttp.send ("
Divdisplay.innerhtml = Xmlhttp.ResponseText;
}
script>
<%}%>
In the above code, where the client's script will create an appropriate COM object, open a website
Www.yoursite.com's connection (using HTTP POST method, synchronization mode), use the Send method to send
An XML fragment then populates the Divdisplay area according to the response on the server (using DHTML here).
The specific execution process is that on the server, the Request object is reproduced into an XML document and then interpreted by the interpreter.
The server responds to the way the XMLHTTP connection is the same as the HTTP connection in any other way, is also used.
Response object. Note that XMLHTTP itself does not check the validity of Request or Response, that is,
The data in the REQUEST or RESPONSE is not required to be an XML document.
"Smart," You may say. "But why don't we use CGI to replace it?" Oh, we have to pay attention
In this way, the entire page is not refreshed if the client-server interaction is used in this way.
We all know that if you do anything through the CGI, you will cause the browser to receive a complete new page, and
This particularly affects visitors on the website, because they have to take all the time to wait for the entire page.
Similarly, CGI is also a burden for website servers because it has to take precious processor cycle cycles and bandwidth.
Consuming all parts of the new page. If this is just once or two times, it doesn't matter, but if it is any
An e-commerce website or a web-based email system, which will mean a lot of the same basic page information
Repeat again and again.
Similarly, if it is used in the website using XML through the CGI, the server will generally have to establish according to data.
XML documents can then be able to process these documents. In this way, the server will cost
A large amount of energy is handling how to construct these XML documents, especially when the amount of data is required.
So if you let the client's browser to create this XML document, and will pass the XMLHTTP after the establishment is completed.
Products are delivered to the server, which will greatly reduce the amount of code and burden on the server.
An example of this type of problem is to add data to the XML document on the server. If you use CGI, then
Just query a CGI Form before you can build an XML node to join the XML document. And if you are using
XMLHTTP needs to be done just to pass this XML node to the server.
The discussion is the basic part of XMLHTTP, about it, you can be in Microsoft Developers
Find an example in NetWork. Although this kind of communication can be used, I am just just in my own use.
A small part is used, maybe any smart developers can discover more experiences.
Using XMLHTTP instead of CGI (even our beloved ASP) can write more ambitious website to use
Program, because now we are concerned with simplicity to just we need to send data. So small code
It is possible to achieve a very complex function, and CGI requires us to finish a new page for each possible operation of the user.
But because all browsers are not all support MSXML, many use ASPs for non-corporate interference
The purpose of the purpose requires support for CGI interaction. But please, write a support of CGI and XMLHTTP data simultaneously.
The page is not very difficult, and the workload of the CGI data is loaded into an XML document is not very large.
The simplest way to determine that the data is CGI's data or data of XML is the MIME type of judging data.
XMLHTTP's MIME is an empty string unless you have other MIME types (you can get this connection)
View different mime type http://msdn.microsoft.com/xml/reference/scriptref/xmlhttpRequest_Object.asp.
And many CGI's MIME type is "Application / X-WWW-FORM-URLENCODED"
Below is a code snippet to judge:
<%
IF (Request.serverVariables ("request_method") == "post")
{
IF (Request.SerVariables) == "Application / X-WWW-FORM-URLENCODED")
{// If it is regular data, let CGI to process
Response.write (Request.form ("stuff"));
}
Else
{// If it is an XMLHTTP connection, turn the Request object into the XML interpreter
Var Req = Server.createObject ("Microsoft.xmLdom");
Req.resolveexternals = false;
Req.validateonparse = false;
Req.async = false;
Req.Load (Request);
Response.write (Req.Documentelement.selectsinglenode ("stuff"). Text);
}
}
Else {%>
This is a method of simply understanding how CGI can also process XMLHTTP data in the same ASP page.
And it also provides a mode that is compatible with IE5.0 on the client and browsing websites using other other browsers.
Or can also take another method, which is what I use the program or use the previous CGI interface.
But use the XMLHTTP method in other types of client applications, such as Microsoft Office.
The function of XMLHTTP is not only limited to the browser, I use XMLHTTP in Microsoft Office's VBA development program.
Have a huge success. Now I assume that I was asked to use XML technology to update the Excel electronic watch on our company server.
Data in the grid. Excel can reprint the HTML table directly into its own table, but it cannot handle the format complex
Pages, such as this page. Data unless you use very clever skills, you can't insert a spreadsheet.
My solution is to write an ASP page to manipulate data passed from Excel through XMLHTTP.
Send a request via a VBA macro to the server, then load the response into the XML document, and then explain it by the interpreter
Insert in the Excel's spreadsheet. This will be a seamless solution, and a simple buttons may make your boss,
Colleagues or any other person's relationship is changed.
Below is a code snippet I have implemented above. It uses XMLHTTP to load information in the server, then
It is inserted into the spreadsheet.
Public Sub Updatesheet ()
DIM XMLHTTPSET XMLHTTP = CREATEOBJECT ("Microsoft.xmlhttp")
Call Xmlhttp.Open ("pos", "http://www.yourserver.com/yourpage.asp", false)
Call Xmlhttp.send ("
DIM XMLDOC
Set Xmldoc = CreateObject ("Microsoft.xmldom")
XMLDoc.async = false
XMLDoc.LoadXml (Xmlhttp.Responsexml)
Worksheets ("Timesheet"). Range ("a1"). Value = Xmldoc.documentelement.getattribute ("firstname")
Worksheets ("Timeesheet"). Range ("B1"). Value = Xmldoc.documentelement.getattribute ("Lastname")
Worksheets ("Timesheet"). Range ("c37"). Value = Xmldoc.documentElement.selectsinglenode ("totalhours"). TEXT
End Sub
This VBA is equally simple. Send a request to the server and insert the response on the server
XML document, then update the contents in Cell in Excel. Of course, we can use this technology to do more use.
For example, from an Office application program to upload data into the XML document on the server (XML should be your preferred format,
Instead of the stupid officeXML implementation provided in Office 2000). Or use XML / ASP as a shell of your database
In this way, your application has a simple, unified interface for the database, and the database structure
Changes don't have to change all your client code.
I found that XMLHTTP object is very useful in my programming. I usually write programs in Visual Basic, Delphi, and Visual J . In this process, I often find that these languages have different methods for processing online programs.
They each have their own handling mechanisms for the network program, but if you use a unity when dealing with the network program
The XMLHTTP connection method, then the code of the connection server will be very small, even more optimized, this is a kind of all
The language is unified.