日期:2014-05-16  浏览次数:20457 次

extjs ajax 与servlet 交互的一个例子
extjs ajax 与servlet 交互的一个例子
==============Servlet================
package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ExtAjaxServlet extends HttpServlet {

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    String entityId = request.getParameter("entityId");
	    System.out.println(entityId);
	    if(entityId.equals("00001")){
	    	response.getWriter().write("{success:true,msg:'成功'}");			
	    }else{
	    	response.getWriter().write("{error:'没有录波文件'}");	
	    	response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);     //必须设置失败标识,否则前端还会调用success函数  
	    }
	}
}

==============js================
Ext.onReady(function(){
	Ext.Ajax.request({
		url:'extAjax',
		params: {entityId: '00002'},

		success:function(response){
	        var respText = Ext.decode(response.responseText); //解码JSON格式数据为一个对象   
	        Ext.Msg.alert('提示', respText.msg); 
		},
		failure:function(response){
			var respText = Ext.decode(response.responseText); //解码JSON格式数据为一个对象   
			Ext.Msg.alert('错误', respText.error);   
		}
	})
})


站在http://wjt276.iteye.com/blog/452365 肩上