XMLHTTP: Website Super Adhesive

Source: Internet
Author: User
Tags format object http post insert interface new features range string
Xml
Introduction: This paper introduces how to use the XMLHTTP to communicate with the server side, and introduces some specific applications.
--------------------------------------------------------------------------------
Introduction to XML 28: XMLHTTP: Website Super Adhesive
Translation: Batman

Brief introduction
Many ASP developers want to be able to use Microsoft's support XML in their own websites
The new features. Among them, some people find that you can use XML to decorate a Web site, but if you just
If you're using only xmldom, you're going to lose something else that's more important. After all, XML is used to
As an image of data representation and data exchange on the Internet. Although using XML can be very satisfying
To depict your data, but developers have to use CGI to do the browser and server
Data exchange unless you are using XML documents on both the browser side and the client.
Of course, CGI is perfectly competent in terms of conveying information, but if and when it comes to XML
Use together to make XML lose a lot of its own usefulness. Fortunately, Microsoft offers a more
Efficient way to transfer XML, although the method is largely not valued.
There are a series of objects in the MSXML interpreter package provided by Microsoft, and perhaps no one will value the
Xmlhttpconnection object. In short, it allows you to open an HTTP connection to the server,
Send some data and retrieve some data. And all of this is done in a few scripts.
The XMLHTTP object is typically used for XML data interchange, but data in other formats is also allowed.

Application in the business process
The standard mode for this type of interchange is that the client sends an XML-formatted text string to the server,
The server then loads the string into a Xmldom object and interprets it, then returns a
HTML to the client, or another piece of XML code to give the client the browser to explain itself.
In this way, it is a very effective form of information transfer, especially when you use
DHTML allows you to dynamically display data based on the return information.
Examples are as follows (can only run when the client and server are fitted with IE5)
<%
if (Request.ServerVariables ("request_method") = = "POST")
{
var req= server.createobject ("Microsoft.XMLDOM");
Req.async=false;
Req.load (Request);
if (req.documentelement.nodename== "timesheet")
{
Do some random processing of the data ...
.....
Response.Write ("}
}
Else
{%>

<div id= "Divdisplay" >the response is put in here</div>
<input type= "button" value= "Send it!" >
<script>
function SendData () {
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
xmlHTTP. Open ("POST", "http://www.yoursite.com/thispage.asp", false);
xmlHTTP. Send ("<timesheet>an impossibly useless timesheet fragment</timesheet>");
Divdisplay.innerhtml=xmlhttp.responsetext;
}
</script>
<%}%>

In the above code, where the client script will create an appropriate COM object, open a Web site in the
Www.yoursite.com connection (using the HTTP POST method, synchronous method), sending using the Send method
An XML fragment, and then populate the Divdisplay region with the response on the server (DHTML is used here).
The specific execution process is that, on the server, the request object is reproduced into an XML document and interpreted by the interpreter.
The server responds to the XMLHTTP connection in the same way that it responds to HTTP connections in any other way, and also uses the
Response object. Note that the XMLHTTP itself does not check the validity of the request or response, i.e.
The data in request or response does not necessarily have to be an XML document.
"Smart," you might say. "But why don't we use CGI instead of it?" "Hehe, we should note
The thing is that if you use this approach to client-server interaction, the entire page is not refreshed.
We all know that if you do anything through CGI, it will cause the browser to receive a complete new page, and
This especially affects visitors on the site because they have to spend all their time waiting for the entire page to be downloaded.
Similarly, CGI is a burden for Web servers because it has to put valuable processor cycles and bandwidth
Consumed in all parts of the new page. It doesn't matter if such an operation is only once or two times, but if it is for any
An E-commerce site or a web-based e-mail system, which would mean a lot of the same basic page information
The load is repeated again and again.
Similarly, if you are using XML in a Web site using CGI, the server will typically have to build on the data
XML documents before you can do what you need to do with these documents. In this way, the server will spend
A great deal of effort is being spent on how to construct these XML documents, especially when the amount of data that needs to be transferred is large.
So if it's the client's browser that builds this XML document, and after the build is done, the last one will be XMLHTTP
The product is delivered to the server, which greatly reduces the amount of code and burden on the service side.
An example of this type of problem is adding data to the XML document on the server. If you are using CGI, then
You need to frequently query a CGI form before you can build an XML node that joins XML documents. And if you are using
The only thing XMLHTTP need to do is pass this XML node to the server.

It says that the XMLHTTP Basic section is discussed, and detailed examples of it are available at Microsoft developers
Find examples in network. Although the use of this means of communication can be very promising, I in my own application is only
Using a very small part of it, perhaps any clever developer can find more examples of usage.

Using XMLHTTP instead of CGI (even our beloved ASP) allows me to write more ambitious web applications
Program, because now we are concerned with simply the data we need to send. So that you can use very little code
Can achieve very complex functionality, and CGI requires us to complete the entire site for every possible operation of the user.
A new page is generated.
However, since not all browsers support MSXML at this time, many of them are written in ASP for non-enterprise internal
The purpose of the application program needs to support CGI interaction. But thankfully, writing a support for both CGI and XMLHTTP data
Pages are not very difficult, and the amount of effort to load CGI data into an XML document is not very significant.
The simplest way to determine whether the data is CGI or XML is the MIME type of the data.
XMLHTTP MIME is an empty string, unless you are specific to the other MIME type (you can go to this connection
View different MIME type http://msdn.microsoft.com/xml/reference/scriptref/XMLHttpRequest_object.asp).
And many CGI MIME types are "application/x-www-form-urlencoded"
The following is a code fragment to judge:

<%
if (Request.ServerVariables ("request_method") = = "POST")
{
if (Request.ServerVariables ("content_type") = = "Application/x-www-form-urlencoded")
{//If it's regular data, let the CGI handle it.
Response.Write (Request.Form ("stuff"));

}
Else
{//If it is a XMLHTTP connection, reprint 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 straightforward way to work with CGI in the same ASP page and to process XMLHTTP data.
It also provides a way to be compatible with IE5.0 installed on the client and browsing the Web site using other browsers.
Or you can take another approach, that is, I used all the Web site applications or the previous CGI interface,
However, other types of client applications use the XMLHTTP method, such as Microsoft Office applications.
XMLHTTP features are not limited to browsers, I use XMLHTTP in Microsoft Office VBA development programs
has been a great success. Now I assume I'm being asked to use XML technology to update the Excel spreadsheet on our server
The data in the grid. Excel is able to reprint HTML tables directly into its own table, but it cannot handle the format of a complex
Pages, such as this page. Data cannot be inserted into a spreadsheet unless it is skillfully used.
My solution was to write an ASP page to manipulate the data passed through XMLHTTP from Excel.
A VBA macro sends a request to the server and then loads the response into an XML document, explained by the interpreter, and then
Insert excel in the spreadsheet. This is going to be a scratch-free solution, and a simple button might make your boss,
The relationship between colleagues or any other person has changed greatly.
Here is a snippet of my code to implement the above method. It uses XMLHTTP to load information from the server and then
into the spreadsheet.

Public Sub Updatesheet ()

Dim XMLHTTP
Set xmlhttp = CreateObject ("Microsoft.XMLHTTP")
Call XMLHTTP. Open ("POST", "http://www.yourserver.com/yourpage.asp", False)
Call Xmlhttp.send ("<reqtimesheet user= ' Jimbob '/>")

Dim xmldoc
Set xmldoc = CreateObject ("Microsoft.XMLDOM")
Xmldoc.async = False
Xmldoc.loadxml (Xmlhttp.responsexml)
Worksheets ("Timesheet"). Range ("A1"). Value = Xmldoc.documentelement.getAttribute ("FirstName")
Worksheets ("Timesheet"). Range ("B1"). Value = Xmldoc.documentelement.getAttribute ("LastName")
Worksheets ("Timesheet"). Range ("C37"). Value = Xmldoc.documentelement.selectSingleNode ("Totalhours"). Text
End Sub

This VBA macro is fairly straightforward. Send a request to the server and insert the response on the server
XML document, and then update the contents of the cell in Excel. Of course, we can use this technique for more use.
For example, upload data from an Office application to an XML document on the server (XML should be your preferred format,
Instead of the stupid Officexml implementations provided in Office 2000. or take xml/asp as a shell for your database.
In this way, your application has a concise, unified interface for the database, and the structure of the database
Changes will not have to change all of your client code.

I found that the XMLHTTP object is very useful in my programming. I generally use visual Basic, Delphi, and Visual J + + to write programs, and in this process, I often find that these languages are very different to the way the online program is handled.
They each have their own processing mechanism for the Web program, but if you are working on a network program using a unified
XMLHTTP connection, then the code to connect to the server will be very small and even more optimized, which is a kind of
Language is a unified interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.