Example of cross-origin request submission using JQuery and Servlet
This article describes how to use JQuery and Servlet to implement cross-origin request submission. For more information, see
Principle: JavaScript Ajax cannot be cross-origin, but requests can be sent to a local Servlet, And the Servlet completes cross-origin. Return the remote structure to the client. In this way, Ajax can be cross-origin. Later, I will release another PHP version. Please pay attention to it. Below is the code
JS Code:
Note: In Post mode, param1 and param2 are the parameter values that are sent remotely. There can be multiple parameters.
The Code is as follows:
// GET Method
Function reqeustCrossDomainProxyGet (){
Var url = "http://www.baidu.com"; // remote request address
Var param = {'requesturl': url, 'typedata': 'json '};
Var data = getCrossDomainProxyRemote (param, "json ");
}
// Post method
Function reqeustCrossDomainProxyPost (param1, param2 ){
Var url = apiServer + "/api/lucene/query ";
Var param = {'requesturl': url, 'typedata': 'json', 'param1': param1, 'param2': param2 };
Var data = getCrossDomainProxyRemote (param, "json ");
}
/**
* JS sends a POST request to a Servlet at the address, all parameters about the remote request.
* The parameter POST is sent to the Servlet.
* @ Param remote Request Parameter
* @ Param rtype JS return type (not used currently)
* @ Return
*/
Function getCrossDomainProxyRemote (param, rtype ){
Var url = "/cross/proxy"; // Servlet URL
Var returndata;
$. Ajax ({
Url: url, type: 'post', dataType: rtype, timeout: 40000, data: param, async: false,
Error: function (response, error) {alert (response. status );},
Success: function (data) {returndata = data ;}
});
Return returndata;
}
Java code:
The Code is as follows:
Public class CorssDomainProxy extends HttpServlet {
Public void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
This. doPost (req, resp );
}
Public void doPost (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
Boolean requestType = false; // indicates the remote request type. The default value is GET.
PrintWriter out = resp. getWriter ();
Enumeration keys = req. getParameterNames (); // retrieve all parameter names passed by the client
ArrayList <String> params = new ArrayList <String> ();
String url = null;
While (keys. hasMoreElements ()){
String key = (String) keys. nextElement ();
/**
* If the request parameters are described as follows, this parameter is not used in remote requests.
*/
If (key. equals ("requesturl") {// determines whether the parameter is a remote request address
Url = req. getParameter (key );
} Else if (key. equals ("typedata") {// you can specify the request data type.
} Else if (key. equals ("returntype") {// you can specify the type of the returned request.
} Else {
Params. add (key); // other parameters added to the List, which are the parameters involved in the remote request.
RequestType = true; // modify the flag. The table finds that the remote request is POST.
}
}
HttpClient client = new HttpClient ();
HttpMethod method = null;
If (requestType) {// determine the request method, and instantiate the HttpMethod object, true: POST, false: GET
Method = new UTF8PostMethod (url );
For (String name: params) {// iterate the POST parameter and add it to the request
String _ value = req. getParameter (name );
(PostMethod) method). setParameter (name, _ value );
}
} Else {
Method = new GetMethod (url );
}
Client.exe cuteMethod (method); // executes the request
String bodystr = method. getResponseBodyAsString (); // The returned result.
Out. println (bodystr); // return the result to the client.
}
/**
* Internal class that converts a URL string to a UTF-8
* @ Author Administrator
*
*/
Private static class UTF8PostMethod extends PostMethod {
Public UTF8PostMethod (String url ){
Super (url );
}
@ Override
Public String getRequestCharSet (){
Return "UTF-8 ";
}
}
}