Ajax form submission in ext2.0

Source: Internet
Author: User

I have read many articles and a lot of code. I can't find the most error. Now let's go to the code. Then let's talk about the points of attention.

 

<% @ 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/> <pead> <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: 'username ', xtype: 'textfield', name: 'name', width: 150, allowblank: True, vtype: 'email '}, <br/> {fieldlabel: 'Password', xtype: 'textfield', name: 'Password', width: 150, inputty PE: 'Password'}, <br/> {fieldlabel: 'birthday', xtype: 'datefield ', name: 'birthday', width: 150} <br/>], <br/> buttons: [<br/> {text: 'login', Handler: Login}, <br/> {text: 'cancel'} <br/>] </P> <p >}); <br/> function login () {<br/> lform. form. submit ({<br/> waitmsg: 'Please log on... ', <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/> </pead> </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/> </ptml> <br/>

 

Servlet code:

 

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/> * this method is called when a form has its tag Value Method equals to get. <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/> * this method is called when a form has its tag Value Method equals to post. <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/>

 

 

 

 

First, in JSP, the method: 'get', URL: 'xxx' is written as action: 'xxx', and errors are always not found. I had no choice but to look at the source code and suddenly saw this property.

 

Second, how does the page determine whether you are success or failure? It depends on the background JSON.

 

That is to say, the background JSON cannot be written in disorder. We should follow this format to {success: true} so that the front-end can know that you have succeeded.

 

 

You can go to {success: True, message: 'login OK '}

In this way, add your own code.

 

 

 

Record it. You are welcome to shoot bricks.

 

I did not say such details on the Internet

 

 

 

This is the case when you get a JSON message at the front end. The two parameters are named form in order, and action is the returned JSON data.

However, JSON. Result starts with JSON. If it is success, alert (action. Result. Success) is displayed as true.

And so on.

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.