EXT2.0中的ajax form提交

來源:互聯網
上載者:User

看了不少文章,也看了不少代碼。始終找不到自己最出錯在什麼地方了,現在直接上代碼吧。然後說一下注意的地方.

 

<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br /><%@include file="/common/taglib.jsp" %><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br /><html><br /> <head><br /> <title>My JSP 'index.jsp' starting page</title><br /><meta http-equiv="pragma" content="no-cache"><br /><meta http-equiv="cache-control" content="no-cache"><br /><meta http-equiv="expires" content="0"><br /><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br /><meta http-equiv="description" content="This is my page"><br /><%@include file="/common/meta.jsp" %><br /><mce:script type="text/javascript"><!--<br />Ext.onReady(function(){<br />var lform = new Ext.form.FormPanel({<br />frame: true,<br />width: 250,<br />height: 130,<br />labelWidth: 60,<br />method: 'post',<br />url: 'servlet/ExtServlet',<br />items:[<br /> {fieldLabel: '使用者名稱', xtype: 'textfield', name: 'name', width: 150, allowBlank: true, vtype:'email'},<br />{fieldLabel: '密碼', xtype: 'textfield', name: 'password', width: 150, inputType: 'password'},<br />{fieldLabel: '出生日期', xtype: 'datefield', name: 'birthday', width: 150}<br />],<br />buttons:[<br />{text: '登入', handler: login},<br />{text: '取消'}<br />]</p><p>});<br />function login() {<br />lform.form.submit({<br />waitMsg: '請稍候正在登入...',<br />success: function(form, action) {<br />alert(action.result.success);<br />},<br />failure: function(form, action) {<br />alert('no');<br />}<br />});<br />};<br />lform.render('grid');<br />});</p><p>// --></mce:script><br /> </head></p><p> <body><br /> <!--<br /> <input type="button" id="b"/> --><br /> <div id="grid" style="margin-left: 20px; margin-top: 5px" mce_style="margin-left: 20px; margin-top: 5px"></div></p><p> </body><br /></html><br />

 

servlet代碼:

 

package com.tsts.sunyanan.ext;</p><p>import java.io.IOException;<br />import java.io.PrintWriter;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>@SuppressWarnings("serial")<br />public class ExtServlet extends HttpServlet {</p><p>private String getResult() {<br />StringBuffer sb = new StringBuffer();<br />//{success:true, message: 'ok'}<br />sb.append("{success:true, message:'a'}");<br />///sb.append("{/"result/":[{/"r/":/"true/", /"m/":/"a/"}]}");<br />return sb.toString();<br />}</p><p>/**<br /> * The doGet method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to get.<br /> *<br /> * @param request the request send by the client to the server<br /> * @param response the response send by the server to the client<br /> * @throws ServletException if an error occurred<br /> * @throws IOException if an error occurred<br /> */<br />public void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {<br />System.out.println("get");<br />response.setContentType("text/x-json");<br />PrintWriter out = response.getWriter();<br />out.println(getResult());<br />out.flush();<br />out.close();<br />}</p><p>/**<br /> * The doPost method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to post.<br /> *<br /> * @param request the request send by the client to the server<br /> * @param response the response send by the server to the client<br /> * @throws ServletException if an error occurred<br /> * @throws IOException if an error occurred<br /> */<br />public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {<br />System.out.println("post");<br />response.setContentType("text/x-json");<br />PrintWriter out = response.getWriter();<br />out.println(getResult());<br />out.flush();<br />out.close();<br />}</p><p>}<br />

 

 

 

 

首先是 jsp中,method: 'get', url: 'xxx',  最開始寫成了 action: 'xxx',總是找不到錯誤。無奈看了一點原始碼忽然看到這個屬性。

 

其次,頁面怎麼判斷你到底是success了還是failure了呢,這個取決於背景json。

 

也就是說背景json也不能亂寫 應該按照這個格式來 {success:true} 這樣前台才知道你是成功了。

 

 

自己可以在{success:true, message:'login ok'}

這樣來添加自己的別的代碼。

 

 

 

記錄一下,歡迎拍磚。

 

網上的確沒有說的這麼詳細的東西

 

 

 

 在前台得到json訊息的時候是這樣子的。參數2個名字按照順序為 form,action  action為返回的json資料

但是json.result才到了json的開始,如果是success的話 alert(action.result.success)  顯示為true。

剩下的以此類推。

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.