// person.find("name").text; 这句话帮忙看下为什么返回不了文本内容?
function but() {
$.ajax({
type : "GET",
url : "/AJAX/CServlet",
dataType : "xml",
success : function(data) {
$(data).find("person").each(function(){
var person = $(this);
var Pname = person.attr("p");//查找person的属性;
var Pvalue = person.find("name").text;
alert(Pname + ", " + Pvalue);
})
}
});
};
@WebServlet("/CServlet")
public class CServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
String xml = "<persons>"+
"<person p='hehe'>"+
"<name>zhangSan</name>"+
"<age>18</age>"+
"<sex>boy</sex>"+
"</person>"+
"</persons>";
System.out.println(xml);
response.getWriter().print(xml);
}
}