The Ajax () method is the underlying AJAX implementation of jquery, which loads remote data through HTTP requests.

Source: Internet
Author: User

  

1 $.ajax ({2         type: "GET", 3         URL: "Handleajaxrequest.action", 4         data: {paramkey:paramvalue}, 5         async: True, 6         dataType: "JSON", 7         success:function (returneddata) {8             alert (returneddata); 9             //callback function after successful request 10             //returneddata--The data returned by the server and processed according to the DataType parameter;             business processing based on returned data         },13         error:function (e) {             alert (e);             //Call this function when the request fails (         }17     }); 18}

Parameter description:

Type: Request method, "POST" or "get", default to "get".

URL: The address where the request was sent.

Data: The Key:value to be passed to the server, written in the form (id:1). A GET request is appended to the URL.

Async: Default True, which is the synchronous request, which is set to false for the asynchronous request.

DataType: Expected data type returned by the server, can not be specified. There are XML, HTML, text, and so on.

In development, the use of the above parameters has been able to meet the basic requirements.

If you need to pass Chinese parameters to the server, you can write the parameters behind the URL and encode them with encodeURI.

  

1 var chinese = "Chinese"; 2 var urltemp = "handleajaxrequest.action?chinese=" +chinese; 3 var url = encodeURI (urltemp);//Encode 4  5 $.ajax ({6         type: "GET", 7         url:url,//Direct-write encoded URL 8         success:funct Ion (Returneddata) {9             alert (returneddata)             , and/or the callback function after the request succeeds             //returneddata--returned by the server and based on DataType The data after the parameter is processed;             business processing based on returned data         },14         error:function (e) {             alert (e);             17//Call this function when a request fails         }18     }); 19}


The Struts2 action handles the request:

  

 1 public void Handleajaxrequest () {2 HttpServletRequest request = Servletactioncontext.getrequest (); 3 H Ttpservletresponse response = Servletactioncontext.getresponse (); 4//Set the return data to HTML text Format 5 response.setcontenttype ("Text/html;charset=utf-8"); 6 Response.setheader ("Pragma", "no-cache"); 7 Response.setheader ("Cache-control", "No-cache"); 8 PrintWriter out =null;             9 try {Ten String Chinese = request.getparameter ("Chinese"); 11//Parameter value is in Chinese and needs to be converted 12 Chinese = new String (chinese.getbytes ("iso-8859-1"), "Utf-8"); System.out.println ("Chinese is:" +chinese); 14 15//Business Processing page resultdata = "Hello World"; Esponse.getwriter (); Out.write (resultdata); 20//If the JSON data is returned, Response.setcontenttype ("application/ Json;charset=utf-8 ")//gson Gson = new Gson ();//string Result = Gson.tojson (resultdata);//Convert data to JSON format with Gson//out.write (result); 25 26           }catch (Exception e) {e.printstacktrace ();}finally {if (out! = null) {30 Out.close (); 31}32}33}

Struts.xml configuration file: No write return type required

  

1 <action name= "handleajaxrequest" class= "com.test.TestAction" 2                 method= "Handleajaxrequest" >3  </ Action>

The Ajax () method is the underlying AJAX implementation of jquery, which loads remote data through HTTP requests.

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.