xml携带数据更多,更加普遍。
服务器返回xml格式的数据:
response.addHeader("Content-Type","text/xml;charset=utf-8"); response.addHeader("Cache-Control","no-cache"); PrintWriter out = response.getWriter(); String username=request.getParameter("username"); String res="<res><mes>"+username+"</mes></res>"; out.print(res); out.flush(); out.close();
ajax如何处理返回数据格式是xml的?
if(myXmlHttpRequest.readyState==4){ //若是返回一个文本 ,则使用responseText 若是返回一个XML,则使用responseXML //window.alert("服务器返回值:"+myXmlHttpRequest.responseText); //如何取出xml的数据 var mess=myXmlHttpRequest.responseXML.getElementsByTagName("mes"); window.alert(mess); var msg=mess[0].childNodes[0].nodeValue;//mess[0] 表示<mes>username</mes> 它的第一个子节点即文本节点,文本节点的nodeValue即文本值。 window.alert(msg); }主要思想就是通过DOM编程,解析xml