AJAX编程实际之与服务器通讯
AJAX编程实际之与服务器通讯
首先看下看下相对简略些的--向服务器发送一个蕴含有名/值对的简略查询串,在这种情况下XHP即可能用GET也可能用POST。
GET
function doRequestUsingGET() {
createXMLHttpRequest();
var queryString = " GetAndPostExample? " ;
queryString = queryString + createQueryString()+ " &timeStamp= " + new Date().getTime();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open( " GET " , queryString, true );
xmlHttp.send( null );
}
POST
function doRequestUsingPOST() {
createXMLHttpRequest();
var url = " GetAndPostExample?timeStamp= " + new Date().getTime();
var queryString = createQueryString();
xmlHttp.open( " POST " , url, true );
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
xmlHttp.send(queryString);
}
queryString就是名/值对的参数方式了(如name=LiLin&age=23),在调用OPEN方法中,当申请方法是用POST的时分为了确保服务器知道申请体中有申请参数,需求调用setRequestHeader,将Content-Type值设置为application/x-www-form-urlencoded.当然也可不放在申请体中(那就不要用POST啦!)
此时server解决:
import java.io. * ;
import java.net. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
public class GetAndPostExample extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String method)
throws ServletException, IOException {
// Set content type of the response to text/xml
response.setContentType( " text/xml " );
// Get the user's input
String firstName = request.getParameter( " firstName " );
String middleName = request.getParameter( " middleName " );
String birthday = request.getParameter( " birthday " );
// Create the response text
String responseText = " Hello " + firstName + " " + middleName
+ " . Your birthday is " + birthday + " . "
+ " [Method: " + method + " ] " ;
// Write the response back to the browser
PrintWriter out = response.getWriter();
out.println(responseText);
// Close the writer
out.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Process the request in method processRequest
processRequest(request, response, " GET " );
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Process the request in method processRequest
processRequest(request, response, " POST " );
}
}
对get and post方法都用processRequest来解决。
要向服务器发送相干简单的查询串,可能将模型变化为XML发送到server 。
client端:
function createXML() {
var xml = " <pets> " ;
var options = document.getElementById( " petTypes " ).childNodes;
var option = null ;
for ( var i = 0 ; i < options.length; i ++ ) {
option = options[i];
if (option.selected) {
xml = xml + " <type> " + option.value + " <\/type> " ;
}
}
xml = xml + " <\/pets> " ;
return xml;
}
function sendPetTypes() {
createXMLHttpRequest();
var xml = createXML();
var url = " PostingXMLExample?timeStamp= " + new Date().getTime();
xmlHttp.open( " POST " , url, true );
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded " );
xmlHttp.send(xml);
}
createXML方法无非就是将内容以DOM的样式存到var xml(变量)里。有时也能够出现client间接将本地的一个XML文件间接以DOM(当然可能edit)的样式传送.(也放这个时个的Content-Type应该为text/xml了!)这时能够要用到ActiveXObject("MSXML2.DOMDocument.3.0")这样一个控件了。
关于这个控件有个方法可能在各broswer中通用的JS代码:
// --------------------------------------------------------------------
// Function: CreateXMLDOM
//
// Purpose: Creates a new XML DOM.
//
// Parameters: None
//
// Returns: XMLDOM object OR null
// --------------------------------------------------------------------
function CreateXmlDOM()
{
var oXML = new ActiveXObject(GetXmlParserProgID());
try
{
oXML.setProperty( " AllowXsltScript " , true );
}
catch (err) {}