Principle: JavaScript Ajax cannot be cross-domain, but it can be done across domains by issuing a request to a servlet in the local area. The remote structure is then returned to the client. This will allow Ajax to cross the domain. In the back, and then send a PHP version, please pay attention AH. Here is the code
JS Code:
Note: In the post mode, param1 and param2 can have more than one parameter value sent to the remote.
Copy Code code as follows:
Get mode
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 ':p aram1, ' param2 ':p aram2};
var data = Getcrossdomainproxyremote (param, "json");
}
/**
* JS sends a POST request to a servlet in this address, all parameters for the remote request.
* Send to Servlet in the post mode here
* @param param remote Request parameters
* @param rtype JS return type (not used for a while)
* @return
*/
function Getcrossdomainproxyremote (param,rtype) {
var url = "/cross/proxy";//servlet URL Address
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:
Copy Code code 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;//mark Remote request type, default to get method
PrintWriter out = Resp.getwriter ();
Enumeration keys = Req.getparameternames ()//Remove all parameter names passed in by the client
arraylist<string> params = new arraylist<string> ();
String Url=null;
while (Keys.hasmoreelements ()) {
String key = (string) keys.nextelement ();
/**
* This parameter does not participate in the remote request if there are several representations within the request parameter
*/
if (Key.equals ("Requesturl")) {//Determine if parameter is, remote request address
url = req.getparameter (key);
}else if (key.equals ("Typedata")) {//Judge request data type, not used for a while
}else if (key.equals ("ReturnType")) {//Judgment request return type, temporarily not used
}else{
Params.add (key)//Other join parameter list, here is the parameter to participate in remote request
RequestType = true;//Modify tag, table asks remote request for POST method
}
}
HttpClient client = new HttpClient ();
HttpMethod method = null;
if (RequestType) {//Judge the request method, and instantiate the HttpMethod object, True:post,false:get
method = new Utf8postmethod (URL);
for (String name:params) {//Iteration post parameter, join to request
String _value = req.getparameter (name);
((Postmethod) method). Setparameter (Name,_value);
}
}else{
method = new GetMethod (URL);
}
Client.executemethod (method);//Execute request
String bodystr = method.getresponsebodyasstring ();//return result
OUT.PRINTLN (BODYSTR)//Returns the result to the client
}
/**
* Inner class, convert URL string to UTF-8
* @author Administrator
*
*/
private static class Utf8postmethod extends Postmethod {
Public utf8postmethod (String URL) {
Super (URL);
}
@Override
Public String Getrequestcharset () {
return "UTF-8";
}
}
}