1, communication mode of C / S
When we discuss the data interaction of the client and the server in the previous article, the direction of the data stream is always from the server to the client, rarely involves the client to send requests to the server and process the return information. 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, XML-based C / S
Communication between C / S using XML is an efficient way of work. 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 transmits XML data;
● The client creates an XMLHTTP object that includes a variety of methods and properties, which can send XML data to applications (such as ASP pages) on the server, and prepare to receive response information;
● The client reproduces the XML packet to the XMLHTTP object and sends it to the ASP page;
● The server performs ASP and creates a server-side XMLDOM object to receive XML data;
● ASP loads the data package to the XMLDOM object of the server side;
● ASP is necessary for XML data and returns a confirmation message;
● The client receives the response message and performs the next step.
3. Send data to the server
The primary task of the client 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.
Here is a dynamically generated XML document:
Set DocSubmit = CreateObject ("Microsoft.xmLDom")
DOCSUBMIT.ASYNC = FALSE
DOCSUBMIT.LOADXML
" XML Version = '1.0'?>" &
TXTCUSTOMERID.VALUE &
Customer> &
" CustomerORDER>"
If the user enters "5" as the user ID, the result of the above program is as follows:
XML Version = '1.0'?>
CustomerORDER>
Next, add XML data to the
For example, there is an XML data island as follows:
XML Version = "1.0"?>
OrderItem>
order>
xml>
Use the DOM technology described earlier to access the
Set docorder = dsoorder.xmldocument
Set nodeorder = docorder.selectsinglenode ("// order")
The
Set nodeordosendosend = nodeorder.clonenode (TRUE)
DOCSUBMIT.DOCUMENTELEMENT.APPENDCHILD NodeRDEndosend
After the above operation, the final data package is:
XML Version = "1.0"?>
OrderItem>
order>
CustomerORDER>
4, XMLHTTP object Open method
After the data packet is completed, you can send the packet to the server using the HTTP Request object. Microsoft.xmlHTTP objects are provided in MSXML to complete the conversion from packets to the REQUEST object, and send tasks.
Creating a statement for the XMLHTTP object is as follows:
Set poster = creteObject ("Microsoft.xmlhttp")
The OPEN method is called after the object is created, and the REQUEST object is initialized. The syntax format is:
Poster.open http-method, url, async, userid, password
The OPEN method contains 5 parameters, the top three are necessary, the latter two are optional (provided when the server needs to be authenticated). The meaning of the parameters is as shown in the following table:
Parameter Description
HTTP-METHOD HTTP communication method, such as GET or POST
The URL receives the URL address of the server of XML data. Usually you want to specify an ASP or CGI program in the URL
ASYNC a Boolean identifier, indicating whether the request is asynchronous. If it is asynchronous communication, the client will not wait for the server's response; if it is synchronous mode, the client will wait until the server returns the message before performing other operations.
UserID user ID for server authentication
Password user password for server authentication
In the following example, the client sent a POST request to the "CustomerOasp" page using asynchronous mode:
Poster.open "Post", "CustomerOrDer.asp", false1, XMLHTTP object SEND method
After initialization of the Request object with the Open method, call the SEND method to send XML data:
Poster.send XML-Data
The parameter type of the 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 is already loaded
2 XML document is already loaded, it is processing
3 section XML document has been parsed
4 Documents have been parsed, and the client can accept the return message.
2, server-side data processing
After receiving the packet sent by the client, the 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 is as follows:
<%
Set docreceived = creteObject ("Microsoft.xmldom")
DOCRECEIVED.ASYNC = FALSE
DocuReceived.Load Request
Set rootnode = docreceived.documentelement
Set nodecustomer = docreceived.selectsinglenode ("// customer")
Customerid = nodecustomer.firstchild.NodeValue
......
%>
3, server-side response message
The server constructs a response message 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 come to see an example of an HTML page, this message page contains information about the customer ordered:
<% For Each Node in ListorderItem
Title = node.getattribute ("Title")
Set quantitynode = node.selectsinglenode ("quantity")
Quantity = QuantityNode.firstchild.nodeValue%>
<% = title%>, <% = quantity%> p>
<% Next%>
The server adopts the XML document as the return message is that the client can use the smart program to analyze structured messages, and more accurately understand the information to be expressed. E.g:
<%
Set DocResponse = CreateObject ("Microsoft.xmLDom")
DocResponse.async = false
DocResponse.load "MyfixedResponse.xml"
Response.contentType = "text / xml"
Response.save DocResponse.save DocResponse
%>
When using an XML document, you must specify the contentType property value 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.
In addition to loading existing XML documents, you can construct XML data in real time as response:
<% Response.contentType = "text / xml"%>
<% For Each Node in ListorderItem
Title = node.getattribute ("Title")
Set quantitynode = node.selectsinglenode ("quantity")
Quantity = QuantityNode.firstchild.nodeValue%>
<% Next%>
shipped>
confirmation>
4, client processing response information
After receiving the return message, the client is received, and there is basically a interactive cycle between C / S. The client receiving response is implemented by the properties of the XMLHTTP object:
● Responsetxt: The message will return as a text string;
● ResponseXML: Treat the return message as an XML document, use XML data in the server response message;
● ResponseSteram: Treats the return message as a Stream object.
The return message of the HTML page is generally processed as a text string. The following example is a complete interaction process, the code of the client script is as follows:
Sub subs_onclick ()
'Creating XMLDOM
Set DocSubmit = CreateObject ("Microsoft.xmLDom")
... 'is constructed as previously described
'Send a packet to the server
Set poster = creteObject ("Microsoft.xmlhttp")
Poster.open "Post", "CustomerORDER.ASP", FALSE
Poster.send docsubmit
'Receive ResOponse Messages
Displayarea.innerhtml = Poster.ResponseText
End Sub
Submit_onclick method first creates an XMLDOM object to load XML data, then create an XMLHTTP object to send a packet to the server in synchronization. An HTML page returns an HTML page as a response message after the server is complete. The XMLHTTP object receives the message according to the text string, and binds it with the DIV element and displays the final result in the browser. The basic procedure of the XML data is the same as the above example, just when receiving, use the responsexml attribute, and then use the DOM technology to further process the XML message. In this way, a complete C / S interaction process is all over.