Jquery AJAX Simple implementation of front-end background data transfer

Source: Internet
Author: User

Ajax is a very powerful thing, before written an Ajax servlet asynchronous interaction, is placed on the URL of the request to implement, placed on the URL is limited in length, if you want to save a long thing, such as blog this is not!

Guide Package:

The main is Json.jar this bag, the other is Ueditor bag!

Front Code: (This is part of the main code, omitting the Ueditor code)

//Introduce some plugins for Easyui<Linkrel= "stylesheet"type= "Text/css"href= "Jquery-easyui-1.3.3/themes/default/easyui.css"><Linkrel= "stylesheet"type= "Text/css"href= "Jquery-easyui-1.3.3/themes/icon.css"><Scripttype= "Text/javascript"src= "Jquery-easyui-1.3.3/jquery.min.js"></Script><Scripttype= "Text/javascript"src= "Jquery-easyui-1.3.3/jquery.easyui.min.js"></Script><Scripttype= "Text/javascript"src= "Jquery-easyui-1.3.3/locale/easyui-lang-zh_cn.js"></Script>the method of submitting the Content function submitcontents () {//ue.geteditor (' editor '). GetContent () This method is the method that the Ueditor gives to get the input box content, realizes some var contents = ue.geteditor (' editor '). GetContent ();
This is a jquery encapsulated Ajax method.
Save.do is the back office address! {Text:contents} The previous one is the key, the latter is the value, from the background with GetParam ... method to get the value! function (result) is a handler that puts something back in the foreground.
Result is a JSON object returned in the background
$.post ("Save.do", {text:contents},function (result) {
                if (result.success) {                    $.messager.alert (' System tip ', ' Operation succeeded! This is what is returned from the background: ' +result.contents ';                } else{                    $.messager.alert (' System tip ', ' Operation not successful! ');}}            , "json");} Button Binding method <type= "button"  value= "Commit"  OnClick= "submitcontents ()">

Background code:

 PackageCom.loger.servlet;Importjava.io.IOException;ImportJava.net.URLDecoder;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.json.JSONObject; Public classSaveextendsHttpServlet {@SuppressWarnings ("Deprecation") @Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO auto-generated Method Stub        
The returned encoding filter, otherwise the reception of the Chinese will be garbled resp.setcharacterencoding ("Utf-8"); String Contents= Req.getparameter ("text");
Defines a JSON object, which is the jsonobject result of the value stored by a key-value pair=NewJsonobject (); if(contents = =NULL) {Result.put ("False",false ); System.out.println ("No content"); }Else{result.put ("Success",true); Result.put ("Contents", contents); SYSTEM.OUT.PRINTLN (contents); } resp.getwriter (). println (Result.tostring ()); } @Overrideprotected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {//TODO auto-generated Method Stub This. DoPost (req, resp); }}

Operation Result:

Jquery AJAX Simple implementation of front-end background data transfer

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.