Home > Article > Web Front-end > Using JQuery and Servlet to implement cross-domain submission request example sharing_jquery
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.
/**
* 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: