【ExtJs】ExtJs的Ajax,extjsajax

來源:互聯網
上載者:User

【ExtJs】ExtJs的Ajax,extjsajax

這東西非常簡單,只要你會用aspx,jsp,php等任意一種伺服器語言或者架構,知道怎麼擷取頁面傳遞參數就行了。

ExtJs的Ajax甚至比Jquery的還要簡短。


一、基本目標

就比如如的一個極其簡單的例子,Ajax.html有一個按鈕,一點擊,傳遞a=10與b=20這兩個值去Helloworld.jsp或者Helloworld.php,然後Helloworld.jsp或者Helloworld.php向前台返回資訊:



二、Jsp版的ExtJs的Ajax

1、首先在Eclipse for JavaEE建立一個Dynamic Web Project,然其自動產生web.xml,並刪去這個web.xml中無關緊要的東西,如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"></web-app>
之後把ExtJs4中所需要的css,js拷貝到WebContent檔案夾下,這東西在《【ExtJs】ExtJs4.2.1的配置與Helloworld》(點擊開啟連結)已經講過,之後的目錄結構如,lib中沒有任何東西,無須任何額外的jar包。


2、建立Ajax.html如下,裡面有一個id為btn1的按鈕,關鍵是引入了ExtJs資源之後的那段指令碼,很簡單的,指明btn1被點擊之後的動作,向Helloworld.jsp這個頁面,傳遞參數a=10,b=20然後以POST的方式傳遞,Helloworld.jsp返回的東西是去除HTML標籤的純文字response.responseText,一般Ajax只傳Json與文本

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Ajax</title><script type="text/javascript" src="js/ext-all.js"></script><script type="text/javascript" src="js/bootstrap.js"></script>                <script type="text/javascript" src="js/ext-lang-zh_CN.js"></script><link href="ext-theme-classic/ext-theme-classic-all.css" rel="stylesheet" type="text/css" ></head><body><button id="btn1" type="button" >請求Helloworld.html</button></body></html><script>Ext.onReady(function(){Ext.get("btn1").on("click", function () {          Ext.Ajax.request({                url: 'Helloworld.jsp',                params: { a: 10, b: 20 },                method: 'POST',                success: function (response, options) {                    Ext.MessageBox.alert('成功', response.responseText);                },                failure: function (response, options) {                    Ext.MessageBox.alert('失敗', '請求逾時或網路故障,錯誤編號:' + response.status);                }            });});});</script>
3、Helloworld.jsp這個頁面更加簡單,僅僅就是JSP版的擷取參數,然後這把個參數列印出來。JSP剛入門的都會。

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Helloworld</title></head><body><%String a=request.getParameter("a");String b=request.getParameter("b");%>Helloworld,傳過來的A為:<%=a%>,傳過來的B為:<%=b%></body></html>

三、php版的ExtJs的Ajax

1、php更加簡單了,連web.xml與WebContent這些煩人的東西都沒有,其目錄結構如下,與上面的基本一樣的


2、Ajax.html就把響應頁面從Helloworld.jsp改成Helloworld.php,其餘一字未改

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Ajax</title><script type="text/javascript" src="js/ext-all.js"></script><script type="text/javascript" src="js/bootstrap.js"></script>                <script type="text/javascript" src="js/ext-lang-zh_CN.js"></script><link href="ext-theme-classic/ext-theme-classic-all.css" rel="stylesheet" type="text/css" ></head><body><button id="btn1" type="button" >請求Helloworld.html</button></body></html><script>Ext.onReady(function(){Ext.get("btn1").on("click", function () {          Ext.Ajax.request({                url: 'Helloworld.php',                params: { a: 10, b: 20 },                method: 'POST',                success: function (response, options) {                    Ext.MessageBox.alert('成功', response.responseText);                },                failure: function (response, options) {                    Ext.MessageBox.alert('失敗', '請求逾時或網路故障,錯誤編號:' + response.status);                }            });});});</script>
3、Helloworld.php也是php剛入門的童鞋都會的,簡單的擷取參數,然後這把個參數列印出來

<?php$a=$_REQUEST["a"];  $b=$_REQUEST["b"];echo "helloworld,傳過來的a為:".$a.",傳過來的b為:".$b;?>

運行結果如下:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.