jQuery--AJAX传递xml
程序代码:
$.ajax({ url:'Accept.jsp', type:'post', //数据发送方式 dataType: 'xml', // 注意这里是xml哦 ,不是html ( html比较简单,所以我拿xml做下例子,解释下 ) data:'text='+$("#name").val()+'&date='+new Date(), //要传递的数据 timeout: 2000, //设置本地超时 .( 毫秒) error: function(){ alert('Error loading XML document'); }, success: function(xml){ $(xml).find("student").each(function(){ var item_text = $(this).text(); // alert($("name" , xml).text()); //选择器注意下 写法 $('<li></li>').html(item_text).appendTo('ol'); }); } });
body:
程序代码
<form id="form1"> name:<input type="text" value="cssrain" id="name"><br> <input type="button" value="save" id="save"> </form> <br> 返回xml: <ol></ol>
Accept.jsp:
String text=request.getParameter("name"); //获取传来的参数 response.setContentType("text/xml"); //注意,由于你是以xml形式传递过来的,所以这里必须写。 out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.print("<student>"); out.print("<name>"+text+"</name>"); out.print("</student>");