Author: Zhang Jian
1. Communication mode of C / S We discuss the data interaction of the client and server in the previous article, the direction of the data stream is always from the server to the client, and rarely involves the client machine to send requests and handle the return information. problem. In fact, in e-commerce, customers send data to the server is also an important link, such as the user who fills in the goods or the like. In a conventional implementation, the user ends as long as it makes a small change in the order, to send messages to the server, requiring update data. This increases the load of the server and the network, which reduces work efficiency. More efficient work mode is to cache changes to the changed information in the client side, then send it to the server separately, so some uncertain modification information is stored in the client, only those data that need to be updated will be updated. Send it out, thereby avoiding many unnecessary operations of the network and servers. 2. Communication based on XML-based C / S using XML is a highly efficient manner. First pack the XML data on the client, then send it to the server as a unit in the XML packet, the server returns the message after processing the data, and the client receives the message to perform other operations, thereby ending a communication cycle. The specific implementation steps are as follows: ● The client makes an XMLDOM object as a carrier that sends XML data; ● The client creates an XMLHTTP object, including a variety of methods and properties, can send XML data to the server (such as ASP) Page), ready to receive response information; ● The client reproduces the XML packet to the XMLHTTP object and sends it to the ASP page; ● The server performs an ASP, and create a server-side XMLDOM object to receive XML data; ● ASP puts the data package On the XMLDOM object of the server; ● ASP performs the necessary processing for XML data, and returns a confirmation message; ● The client receives the response message and performs the next step. 3. The primary task of sending a data client to the server is to construct an XML packet. XMLDOM as a carrier of the packet has a source of data such as any XML document or a fragment of the XML document (such as XML data island), and can even use the loadXML method to receive the user input information XML document. Below is a dynamically generated XML document: set docsubmit = createObject ("Microsoft.xmLDom") DOCSUBMIT.ASYNC = false docsubmit.LoadXML " Xml version = '1.0'?>" & "
For example, there is an XML data island as follows:
In the following example, the client sent a POST request to the "CUSTOMERORDER.ASP" page using an asynchronous manner: Poster.Open "Post", "CustomerORDER.ASP", false1, XMLHTTP object Send method to use the open method to Request After the object is initialized, call the Send method to send XML data: The parameter type of the POSTER.SEND XML-DATA Send method is Variant, which can be a string, a Dom tree or any data stream. The way to send data is divided into synchronous and asynchronous. In an asynchronous mode, once the data package is sent, end the Send process, the client performs other operations; and in the synchronization mode, the client will wait until the server returns a confirmation message to end the Send process. The ReadyState property in the XMLHTTP object reflects the progress of the server during processing the request. The program of the client can set the corresponding event processing method according to this status information. The attribute value and its meaning are shown in the following table: Value Description 0 Response object has been created, but the XML document upload process has not ended 1 XML document has been loaded 2 XML document has been loaded, and the 3 part XML document has parsed 4 documents already After the analysis is complete, the client can accept the return message
2. After receiving the packets sent by the client, the server-side data processing server will immediately process the data and make a corresponding response. The server first creates an XMLDOM object and then loads the data in the Request object and starts accessing XML data via an XMLDOM object. After obtaining XML data, the first thing to do is to verify the XML document (this part of the specific process we will introduce the XML Schema when it will be described later). Once verified, the DOM interface can be used to analyze the XML data (for example, using the extracted information to update records in the database). A simple ASP script as follows: <% Set docReceived = CreateObject ( "Microsoft.XMLDOM") docReceived.async = False docReceived.load Request Set rootNode = docReceived.documentElement Set nodeCustomer = docReceived.selectSingleNode ( "// customer") customerID = NodeCustomer.firstchild.nodeValue ......%> 3, the server-side response message server is constructed after processing XML data, and returns to the client. The form of the message can be a plain text, an HTML page, an XML document, or an HTML page embedded in XML data island. First, let's take an example of an HTML page. This message page contains information about the customer ordered:
<% = title%>, <% = quantity%> p> <% next%> The server uses XML documents as return messages The advantage is that the client can use the smart program to analyze structured messages, and more accurately understand the information to be expressed by the server. For example: <% Set docResponse = CreateObject ( "Microsoft.XMLDOM") docResponse.async = False docResponse.load "MyFixedResponse.xml" Response.ContentType = "text / xml" Response.save docResponse%> in the use of XML documents, you must The value of the contentType property is specified as "text / XML" before filling the response content, indicating the format of the response message is XML. The use of the Save method is to populate the XML document content into the Response object.