Ext.Ajax.request 让前后台数据传输更方便

1、给出jsp.

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
%>



  
   
   
   
  
  
    

2、给出test.js。

function ajax(){
    Ext.Ajax.request({
           url: 'extjsAjax.do',
           success: function(response){
                    var obj = Ext.decode(response.responseText);
                    var  personinfo=obj.name+"'s id is "+obj.id[0];
                    alert(personinfo);
                    alert(obj.reqInfo);
                        },
           failure: function(data){alert("失败")},
           params: { id: 'bar',name:'huangyz',age:25 }
        });
}
Ext.onReady(function(){
      new Ext.Button({text:"添加",
          pressed:true,
          heigth:30,
          renderTo:"btn",
          handler:ajax});
});

3、给出java类。

package com.huangyz.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ExtjsAjax extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        String reqInfo= request.getParameter("id")+"_"+request.getParameter("name")+"_"+request.getParameter("age");
         response.setContentType("text/plain;charset=utf-8");
         try{
         response.getWriter().write("{id:[1,2,3,4,5,6,7,8,9],name:'huangyz',grade:{english:70,math:90},reqInfo:"+"'请求信息是_"+reqInfo+"'}");
         }catch(Exception e){
             e.printStackTrace();
         }
        return null;
    }
}

4、给出struts配置文件。

 
 
 
   
   
   
   
   
    
   
   

5、利用ext,比jquery更方便,个人观点!

你可能感兴趣的:(Ext.Ajax.request 让前后台数据传输更方便)