Using XMLHTTP can realize new data for no new automatic access servers in the page, which can be easily achieved in some chat rooms. Similarly, you can implement timing lysted new page data in other BS applications. However, some problems will be encountered when using XMLHTTP and DomDocument. One is how to transfer parameters, and the second is Chinese garbled problems, and there is how to resolve XML code.
1. Parameter Transfer and Get Parameter Values XMLHTTP's Send Method Send (Varbody) Varbody: Instruction Set. Can be XML format data, or a string, stream, or an unsigned integer array. It can also be omitted, and the instruction will be submitted through the URL parameter of the OPEN method. The Send method can also transmit parameter pairs, and each parameter can be used with & separation, such as var post = "ID = 1000 & Page = 3"; XMLHttp.send (POST);
Or add to the URL parameter of the Open method, such as XMLHTTP.Open ("POST", "http: // localhost: 8088 / getData? Id = 1000 & Page = 3", FALSE);
In JSP or servlet, you can get the parameter string id = (string) Request String ID = (String) in peacetime; string page = (string) Req.getParameter ("Page");
Because the request sent by XMLHTTP is not a Multipart / Form-Data format, it is noted that some methods in the servlet cannot use multiple Multipart / Form-Data formats.
2, Chinese garbled problem XMLHTTP is sent to request and receive return values in UTF-8, so if there is no coding process, in the transmission of the parameters or in the received return value, then we see It is garbled to process the parameters sent.
Var post = "years = 2004-2005 & termid = autumn and winter"; // contains Chinese XMLHTTP.Open ("post", "http: // localhost: 8088 / getdata", false); XMLHttp.SetRequestHeader ("Content-Type", "Application / X-WWW-FORM-URLENCODED"); // To have this sentence xmlhttp.setRequestHeader ("Content-length", post.length; xmlhttp.send (post);
When the parameter is taken in the servlet, the Request is encoded to process Req.SetCharacterencoding ("UTF-8"); then take the parameter string years = (string) Req.getParameter ("years"); string termid = (String) Req.getParameter ("Termid");
If there is an output data to the requested page in the servlet, if you include Chinese, you should also encode the Response Static Final String Content_Type = "text / html; charset = utf-8"; res. setContentType (Content_Type);
Use UTF-8 in the output XML code ("