Use ajax () to interact with the background, and use ajax to interact with the background
The ajax () method is the underlying ajax Implementation of jQuery and loads remote data through HTTP requests.
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 10 after the request is successful // returnedData -- the data returned by the server and processed according to the dataType parameter; 11 // perform business processing based on the returned data 12 }, 13 error: function (e) {14 alert (e); 15 // call this function when the request fails 16} 17}); 18}
Parameter description:
Type: Request Method, "POST" or "GET". The default value is "GET ".
Url: the address where the request is sent.
Data: The data to be transmitted to the server, which has been written in the form of key: value (id: 1 ). The GET request is appended to the url.
Async: The default value is true. It is an asynchronous request. If it is set to false, it is a synchronous request.
DataType: The expected data type returned by the server, which can be left unspecified. Such as xml, html, and text.
In development, the above parameters can meet basic requirements.
If you need to pass Chinese parameters to the server, you can write the parameters after the url and use encodeURI encoding.
1 var chinese = "chinese"; 2 var urlTemp = "handleAjaxRequest. action? Chinese = "+ chinese; 3 var url = encodeURI (urlTemp); // encode 4 $. ajax ({6 type: "GET", 7 url: url, // directly write the encoded url 8 success: function (returnedData) {9 alert (returnedData ); 10 // callback function 11 after the request is successful // returnedData -- the data is returned by the server and processed according to the dataType parameter; 12 // perform business processing based on the returned data 13 }, 14 error: function (e) {15 alert (e); 16 // call this function when the request fails 17} 18}); 19}
The struts2 action processes the request:
1 public void handleAjaxRequest () {2 HttpServletRequest request = ServletActionContext. getRequest (); 3 HttpServletResponse response = ServletActionContext. getResponse (); 4 // set the returned 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 {10 String chinese = request. g EtParameter ("chinese"); 11 // The parameter value is chinese and must be converted to 12 chinese = new String (chinese. getBytes ("ISO-8859-1"), "UTF-8"); 13 System. out. println ("chinese is:" + chinese); 14 15 // Service Processing 16 17 String resultData = "hello world"; 18 out = response. getWriter (); 19 out. write (resultData); 20 // If json data is returned, response. setContentType ("application/json; charset = UTF-8"); 21 // Gson gson = new Gson (); 22 // String result = gson. toJson (result Data); // use Gson to convert Data to json format 23 // out. write (result); 24 out. flush (); 25 26} catch (Exception e) {27 e. printStackTrace (); 28} finally {29 if (out! = Null) {30 out. close (); 31} 32} 33}
Struts. xml configuration file: No need to write the return type
1 <action name="handleAjaxRequest" class="com.test.TestAction"2 method="handleAjaxRequest">3 </action>