XMLHTTP: website super Adhesive

Source: Internet
Author: User
Microsoft. NET: XMLHTTP: website super Adhesive
Posted on Saturday, June 05 @ 11:25:57 CST by joezxh

Joe. Zhang shipping "Overview
Many ASP developers want to use the XML-supported new functions provided by Microsoft on their websites. Some people may find that they can use XML to decorate websites. However, if you only use xmldom, you will lose some more important things. After all, XML is used as an image for online data presentation and data exchange. Although XML is quite satisfactory
But developers have to use CGI to exchange data between the browser and the server, unless you use XML documents on both the browser and client.
Of course, CGI is fully competent in terms of information transmission, but if it is used together with XML, XML will lose a lot of its own use. Fortunately, Microsoft provides a more effective way to transmit XML, although this method is largely ignored.
There are a series of objects in the MSXML interpreter package provided by Microsoft. Maybe no one will pay attention to the xmlhttpconnection object. In short, it allows you to open an HTTP connection to the server,
Send and retrieve some data. All of this can be implemented in a few scripts. XMLHTTP objects are usually used for XML data exchange, but data in other formats is also allowed.

In businessProgramApplication in
The standard mode of this switching type is that the client sends an XML text string to the server, then the server loads the string into an xmldom object and interprets it, and then returns an HTML string to the client, or another XMLCodeLet the client's browser explain it to itself.
This method is very effective for information transmission, especially when DHTML allows you to dynamically display the returned information.
Example: (only when ie5 is installed on the client and server)

In the above Code, the client script will create an appropriate COM object to open
The connection of www.yoursite.com (the POST method of HTTP is used, and the synchronization method is used), and The send method is used for sending.
An XML segment, and then fill the divdisplay area according to the response on the server (DHTML is used here ).
The specific execution process is that on the server, the request object is reprinted into an XML document and then interpreted by the interpreter.
The server responds to the XMLHTTP connection in the same way as the HTTP connection in any other way.
Response object. Note that XMLHTTP does not check the validity of the request or response, that is
Request or the data in response does not need to be an XML document.
"Smart," you may say. "But why don't we use CGI instead ?" Please note that
This means that the entire page is not refreshed when the client-server interaction is performed in this way.
We all know that if we do anything through CGI, it will inevitably lead to the browser to receive a complete new page, and
This particularly affects visitors on the website because they have to spend all their time waiting for the download of the entire page.
Similarly, CGI is also a burden on website servers because it has to take valuable processor cycle and bandwidth
All parts consumed on the new page. If this operation is performed only once or twice, it doesn't matter, but if it is for any
An e-commerce website or a Web-based email system means a large amount of basic page information.
It is repeatedly loaded.
Similarly, if you use XML in website applications through CGI, the server generally has to create
Before you can process these documents as needed. In this way, the server will spend
A lot of effort is spent on how to construct these XML documents, especially when the amount of data to be transferred is large.
Therefore, if you want the client browser to create this XML document, and after the creation is complete, use XMLHTTP
The product is delivered to the server, which greatly reduces the amount of code and burden on the server.
An example of this type of problem is to add data to XML documents on the server. If CGI is used
You need to frequently query a cgi form before you can build an XML node that is added to the XML document. If you use
What XMLHTTP needs to do is to pass the XML node to the server.

The above section discusses the basic part of XMLHTTP. For detailed examples, you can go to Microsoft developers
Network. Although this communication method can be used, I only
A few examples are used. Maybe any smart developer can find more examples.

Using XMLHTTP instead of CGI (even our beloved ASP) enables me to write more ambitious websites
Program, because what we are concerned about now is simply the data we need to send. Therefore, few codes are used.
It can implement very complex functions, and CGI requires us to complete every possible operation of the user.
Generate a new page.
However, not all browsers currently support MSXML, many of which are written using ASP for non-enterprise internal
The target application must support CGI Interaction. However, we are pleased to write a program that supports both CGI and XMLHTTP data.
The page is not very difficult, and it does not take a lot of effort to load CGI data into XML documents.
The simplest way to determine whether the data is CGI data or XML data is to determine the MIME type of the data.
The mime of XMLHTTP is an empty string, unless you specify other MIME types (you can connect to this
View different MIME type http://msdn.microsoft.com/xml/reference/scriptref/XMLHttpRequest_object.asp ).
Many cgi mime types are "application/X-WWW-form-urlencoded"
The following is a code snippet to judge:

If (request. servervariables ("request_method") = "Post ")

{

If (request. servervariables ("content_type") = "application/X-WWW-form-urlencoded ")

{// For general data, use CGI for processing

Response. Write (request. Form ("stuff "));

}

Else

{// If it is an XMLHTTP connection, reload 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.doc umentelement. selectsinglenode ("stuff"). Text );

}

}

Else {%>

This is a simple and clear way to process CGI or XMLHTTP data on the same ASP page.
It also provides a mode that is compatible with ie5.0 installed on the client and allows you to browse websites using other browsers.
Or another method can be used, that is, I still use the previous CGI interface for all website applications,
However, the XMLHTTP method is used in other client applications, such as Microsoft Office applications.
XMLHTTP is not limited to browsers. I use XMLHTTP in Microsoft Office VBA development programs.
Great success was made. Now I assume that I was asked to update the Excel spreadsheet on my company server using XML technology.
Data in the grid. In Excel, HTML tables can be directly reproduced to their own tables, but they cannot process complicated formats.
Page, such as this page. Data cannot be inserted into workbooks unless you use clever techniques.
My solution is to compile an ASP page to manipulate the data transferred from Excel via XMLHTTP.
Use a VBA macro to send a request to the server, load the response to the XML document, explain it through the interpreter, and then
Insert a workbook in Excel. This will be a seamless solution. A simple button may make your boss,
Relationships between colleagues and anyone else have greatly improved.
The following is a code snippet for implementing the above method. It uses XMLHTTP to load information on the server, and then
It is inserted into the workbook.

Public sub updatesheet ()

Dim XMLHTTP
Set XMLHTTP = Createobject ("Microsoft. XMLHTTP ")
Call XMLHTTP. Open ("Post", "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.doc umentelement. getattribute ("firstname ")
Worksheets ("timesheet"). Range ("B1"). value = xmldoc.doc umentelement. getattribute ("lastname ")
Worksheets ("timesheet"). Range ("C37"). value = xmldoc.doc umentelement. selectsinglenode ("totalhours"). Text
End sub

The Macro of this VBA is quite simple. Send a request to the server and insert the response on the server
XML document, and then update the cell content in Excel. Of course, we can use this technology for more use.
For example, uploading data from an office application to an XML document on the server (XML should be your preferred format,
Rather than the stupid officexml implementation provided in Office 2000). Or use XML/ASP as the shell of your database.
In this way, your application has a simple and unified interface for the database, while
You do not need to modify all your client code.

I found that XMLHTTP objects are very useful in my programming. I usually use Visual Basic, Delphi, and Visual J ++ to write programs. In this process, I often find that these languages have different processing methods for online programs,
They have their own processing mechanisms for network programs, but if you use uniform processing for network programs
The XMLHTTP connection method, the code to connect to the server will be very small, or even more optimized, This Is
Unified language interfaces.
"

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.