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 after successful request 10 // 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 // This function is called 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 ); // Encoding 4 5 $. Ajax ({ 6 Type: "Get" , 7 URL: URL, // Directly write the encoded URL 8 Success: Function (Returneddata ){ 9 Alert (returneddata ); 10 // Callback Function after successful request 11 // Returneddata -- the data 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 // This function is called 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. getparameter ("Chinese" ); 11 // The parameter value is in Chinese and must be converted. 12 Chinese = New String (Chinese. getbytes ("ISO-8859-1"), "UTF-8" ); 13 System. Out. println ("Chinese is:" + Chinese ); 14 15 // Business 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 = new gson (); 22 // String result = gson. tojson (resultdata); // 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 <ActionName= "Handleajaxrequest"Class= "Com. Test. testaction"2 Method= "Handleajaxrequest">3</Action>