程序代码

$.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>");

好了, 例子 就到这里, 应该能看懂把.   现在是返回xml.   不过听说json满流行的.   以后在做一些json传递的例子.
posted on 2009-11-23 23:46  钱途无梁  阅读(2296)  评论(0编辑  收藏  举报