1、"responseText”属性以字符串形式返回HTTP响应;“responseXML”属性以XML形式返回HTTP响应。
function
getTel() {
var telText =
document.getElementById("mantel");
telText.value =
"";
if(xmlHttp.readyState == 4) {
if (xmlHttp.status ==
200) {
var message =
xmlHttp.responseText;
telText.value =
message;
}
}
}
2、“responseXML”属性返回了一份XML文档对象,可以使用W3C
DOM节点树方法和属性对该XML文档对象进行检查和解析。
function getUserInfo() {
var
peoplemobile=document.getElementById("_Peoplemobile");
if(xmlHttp.readyState
== 4) {
if (xmlHttp.status == 200) {
var xmlDoc =
xmlHttp.responseXML.documentElement;
var
xSel =
xmlDoc.getElementsByTagName("select");//得到xml文档中,节点为select的对象
for
(var i=0;i<xSel.length;i++){
var
xValue =
xSel[i].childNodes[0].firstChild.nodeValue;//得到select节点下的第一个节点
var
xText =
xSel[i].childNodes[1].firstChild.nodeValue; //得到select节点下的第二个节点
if(xValue==‘peoplemobile‘){
peoplemobile.value=xText;
}
}
}
}
}
二、java中的代码
/**
* ajax获得用户电话号码,返回文本
* @param mapping
*
@param form
* @param request
* @param response
*
@return
* @throws DataAccessException
* @throws
IOException
* @throws ParseException
*/
public
ActionForward toGetTelNumber(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse
response)
throws DataAccessException, IOException,
ParseException {
String username =
request.getParameter("person_id");
String tel =
releasePlanService.getTelByName(username);
if (tel != null)
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out =
response.getWriter();
out.print(tel);
return
null;
} else {
tel =
"";
response.setContentType("text/xml;charset=UTF-8");
PrintWriter
out =
response.getWriter();
out.print(tel);
return
null;
}
}
/**
* ajax获得用户信息,返回xml
* @param
mapping
* @param form
* @param request
* @param
response
* @return
* @throws DataAccessException
*
@throws IOException
* @throws ParseException
*/
public ActionForward togetUserInfo(ActionMapping mapping, ActionForm
form,
HttpServletRequest request, HttpServletResponse
response)
throws DataAccessException, IOException,
ParseException {
String userid =
request.getParameter("userid");
response.setContentType("text/xml;charset=UTF-8");
response.setHeader("Cache-Control",
"no-cache");
String xml_start = "<?xml version = \"1.0\"
encoding = \"UTF-8\"?>";
xml_start +=
"<selects>";
String xml_end =
"</selects>";
String xml = "";
String
last_xml = "";
try {
UserInfoExtendBean
userInfoExtendBean=userInfoService.select(userid);
String
mobile=userInfoExtendBean.getMobile()!=null?userInfoExtendBean.getMobile():"无";
String
tel=userInfoExtendBean.getTel()!=null?userInfoExtendBean.getTel():"无";
String
mail=userInfoExtendBean.getEmail()!=null?userInfoExtendBean.getEmail():"无";
xml
= xml + "<select><value>" + "peoplemobile"
+
"</value><text>" + mobile
+
"</text></select>"
+
"<select><value>" + "peopletel"
+
"</value><text>" + tel
+
"</text></select>"
+
"<select><value>" + "email"
+
"</value><text>" + mail
+
"</text></select>";;
last_xml = xml_start + xml
+ xml_end;
response.getWriter().print(last_xml);
}
catch (Exception e) {
return
mapping.findForward("error");
}
return null;
}