首先来看一个Jquery的onload的方法:
$(document).ready(function() {
alert("aaaaa");
});
接下来进入主题了,Jquery Ajax来解析XML数据,我也是新手,今天刚刚有时间试验了一下..
- 加载Jquery包<script type="text/javascript" src="<%=webRoot%>/jquery-1[1].2.6.pack.js"></script>
- $.ajax({
url:'/platform/contact/resetStatus.do?actionType=test', //异步方法
type: 'POST',
dataType: 'xml',//这里可以不写,但千万别写text或者html!!!
data: "&name=John", //传入的那么参数
timeout: 1000, //延迟1秒执行
error: function(){ //返回失败后
$('#rere').value("请稍候……");
alert('Error loading XML document');
},
success: function(xml){ //返回成功
$(xml).find("callcenter_response").each(function(){ //找到根节点
var id=$(this).children("policy_id"); //节点名称
var policy_id=$(this).children("policy_id").text(); //节点值
var policy_code=$(this).children("policy_code").text(); //节点值
alert(policy_id);
});
}
}); - public void test(HttpServletRequest request, HttpServletResponse response)
throws Exception {
PrintWriter out = response.getWriter();
response.setContentType("text/xml");
String xml = "<?xml version='1.0' encoding=/'UTF-8/'?><callcenter_response>";
xml+="<policy_id>19049059</policy_id><policy_code>000486474153008</policy_code></callcenter_response>";
out.write(xml);
//out.print(xml); //用这么返回xml好像也可以,具体还没有细看呢.
out.flush();
out.close();
}