Principle: JavaScript's Ajax cannot cross domains, but it can complete the cross-domain by sending a request to a local Servlet. Then return the remote structure to the client. This way Ajax can work across domains. I will release a PHP version later, please pay attention. Below is the code
JS code:
Note: In Post mode, param1 and param2 are parameter values sent to the remote, and there can be multiple.
//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 this address, with all parameters related to the remote request.
* Use POST method here to send to Servlet
* @param param remote request parameters
* @param rtype JS return type (not used yet)
* @return
*/
function getCrossDomainProxyRemote(param,rtype){
var url = "/cross/proxy";//Servlet’s 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:
public class CorssDomainProxy extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(re q, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
boolean requestType = false;//Mark remote request type, default is GET method
PrintWriter out = resp.getWriter();
Enumeration keys = req.getParameterNames();//Get all parameter names passed in by the client
ArrayList params = new ArrayList();
String url=null;
while (keys.hasMoreElements()){
String key = (String) keys.nextElement();
/**
* If the request parameter contains the following expressions, this parameter does not participate in the remote request
*/
if(key. equals("requesturl")){//Determine whether the parameter is, remote request address
url = req.getParameter(key);
}else if(key.equals("typedata")){//Judge Request data type, not used for the time being
}else if(key.equals("returntype")){//Determine the request return type, not used for the time being
}else{
Params.add (key); // Other additional parameters list, here is the parameters participating in the remote request
RequestType = TRUE; // Modify the mark, and the remote request is the post method
}
}}
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 parameters and add them to the request
String _value = req.getParameter(name);
((PostMethod )method).setParameter(name,_value);
method);//Execute the request
String bodystr = method.getResponseBodyAsString();//Return the result
out.println(bodystr);///Return the result to the client
*/
private static class UTF8PostMethod extends PostMethod {
public UTF8PostMethod(String url) {
super(url);
}
@Override
public String getRequestCharSet() {
return "UTF-8";
}
}