Ext.Ajax是Ext.data.Connection的一个子类,提供了用简单的方式进行Ajax的功能实现
- (function(){
- Ext.onReady(function(){
- Ext.Ajax.request({
- url:'person.jsp',
- params:{id:'01'},
- method:'POST',
- timeout:3000,
- success:function(response,options){
- console.log(eval(response.responseText)[0].name);
- },
- failure:function(response,options){
- console.log(response.responseText,options);
- }
- });//静态方法
- });
- })();
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- String id = request.getParameter("id");
- System.out.println();
- if("01".equals(id)){
- response.getWriter().write("[{id:'01',name:'uspcat.com',age:'26',email:'[email protected]'}]");
- }
- %>
uspcat.com |
- (function(){
- Ext.onReady(function(){
- Ext.get('b').on('click',function(){
- Ext.Ajax.request({
- url:'person.jsp',
- params:{id:'01'},
- method:'POST',
- timeout:3000,
- form:"myform",
- success:function(response,options){
- console.log(eval(response.responseText)[0].name);
- },
- failure:function(response,options){
- console.log(response.responseText,options + " " + options );
- }
- });//静态方法
- });
- });
- })();
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>ajax</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="ajaxform.js"></script>
- </head>
- <body>
- <form id="myform">
- Name:<input type="text" name="name"/><br/>
- Pass:<input type="password" name="pass"/><br/>
- <input type="button" value="login" id="b">
- </form>
- </body>
- </html>
- (function(){
- Ext.onReady(function(){
- var data = "[{id:'01',name:'uspcat.com',age:'26',email:'[email protected]'}]";
- Ext.Ajax.request({
- url:'person2.jsp',
- method:'POST',
- timeout:3000,
- jsonData:data,//向后台传的参数
- success:function(response,options){
- console.log(eval(response.responseText)[0].name);
- },
- failure:function(response,options){
- console.log(response.responseText,options);
- }
- });//静态方法
- });
- })();
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.getWriter().write("[{id:'01',name:'uspcat.com',age:'26',email:'[email protected]'}]");
- %>
- Ext.onReady(function(){
- Ext.get('b1').on('click',function(){
- var time = Ext.get('time').getLoader();//Ext.ElementLoader
- time.load({
- url:'elementLoader.jsp',
- renderer:function(loader,response,request){
- console.log(response.responseText);
- var time = response.responseText;
- Ext.getDom("time").value = time;
- }
- });
- });
- });
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>ajax</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="elementLoader.js"></script>
- </head>
- <body>
- <input type="text" name="time" id="time"/><br/>
- <input type="button" value="get time" id="b1"/>
- </body>
- </html>
- <%@page language="java" import="java.util.*" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.setCharacterEncoding("UTF-8");
- response.getWriter().write("2011年03月10日");
- %>
- Ext.onReady(function(){
- Ext.get("btn").on("click",function(){//Ext.ElementLoader
- //对每个对象可以设置各种事件
- var i = Ext.get('i').getLoader();
- i.startAutoRefresh(1000,{
- url:'elementLoaderStartAutoRefresh.jsp',
- renderer:function(loader,response,request){
- console.log(response.responseText);
- var i = response.responseText;
- Ext.getDom("i").value = i;
- }
- });
- });
- });
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>ajax</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="elementLoaderStartAutoRefresh.js"></script>
- </head>
- <body>
- <input type="text" name="i" id="i"/><br/>
- <input type="button" value="get i" id="btn"/>
- </body>
- </html>
- <%@page language="java" import="java.util.*" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- int i = 0;
- if(session.getAttribute("i")!=null){
- i = Integer.parseInt(session.getAttribute("i").toString()) + 1;
- session.setAttribute("i",i);
- }else{
- session.setAttribute("i",i);
- }
- response.setCharacterEncoding("UTF-8");
- response.getWriter().write("计算器:" + i++);
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>ajax</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="menu.js"></script>
- </head>
- <body>
- <select name="city" id="city">
- <option value="beijing" selected>北京市</option>
- <option value="tianjing">天津市</option>
- </select><br/>
- <select name="area" id="area">
- <option value="def" selected>----------</option>
- </select>
- </body>
- </html>
- (function(){
- Ext.onReady(function(){
- function createChild(value,name){
- var el = document.createElement("option");
- el.setAttribute("value",value);
- el.appendChild(document.createTextNode(name));
- return el;
- }
- //1.得到city这个dom对象
- var cityObj = Ext.get("city");
- //2.我们为这个city对象注册一个change
- cityObj.on("change",function(e,select){
- //3.得到改变后的数值
- var ids = select.options[select.selectedIndex].value;
- //3.1它先去前台的缓存变量中查数据,当没有的时候去后台拿
- //if(data[city]){
- //直接操作
- //}else{
- //做ajax
- //}
- //4.ajax请求把后台数据拿过来
- Ext.Ajax.request({
- url:'menu.jsp',
- params:{'ids':ids},
- method:'post',
- timeout:4000,
- success:function(response,opts){
- var obj = eval(response.responseText);
- //5.得到地区的那个对象area
- var oldObj = Ext.get("area").dom;//elememt是不能操作,dom是可以操作的
- //6.清除里面项
- while(oldObj.length > 0){
- oldObj.options.remove(oldObj.length-1);
- }
- //7.加入新的项
- Ext.Array.each(obj,function(o){
- Ext.get("area").dom.appendChild(createChild(o.valueOf(),o.name));
- });
- }
- });
- });
- });
- })();
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.setCharacterEncoding("utf-8");
- String ids = request.getParameter("ids");
- if("beijing".equals(ids)){
- response.getWriter().write("[{name:'海淀',value:'01'},{name:'东城',value:'02'},{name:'顺义',value:'03'}]");
- }else if("tianjing".equals(ids)){
- response.getWriter().write("[{name:'和平',value:'01'},{name:'河西',value:'02'},{name:'河东',value:'03'}]");
- }
- %>