It's interesting to see a lot of jquery cross-domain request articles on the Web. Here I publish a servlet and jquery configuration to implement the cross-domain code for everyone's reference. Please advise me of the shortcomings
Principle: JavaScript Ajax cannot cross domains, but it can be done across domains by making a request to a local servlet. The remote structure is then returned to the client. This way, Ajax can be cross-domain. In the back, send a PHP version, please pay attention to AH. Here is the code
JS Code:
Note: In post mode, param1 and param2 can have more than one parameter value sent to the remote.
123456789101112 |
//GET方式
function reqeustCrossDomainProxyGet(){
var url =
"http://www.baidu.com"
;//远程请求地址
var param = {
‘requesturl‘
:url,
‘typedata‘
:
‘JSON‘
};
var data = getCrossDomainProxyRemote(param,
"json"
);
}
//Post方式
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"
);
}
|
1234567891011121314151617 |
/**
* JS向本址的一个Servlet发送POST请求,所有关于远程请求的参数。
* 在此处参POST方式发送给Servlet
* @param param 远程请求参数
* @param rtype JS返回类型(暂时没有用到)
* @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:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
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
;
//标记远程请求类型,默认为GET方式
PrintWriter out = resp.getWriter();
Enumeration keys = req.getParameterNames();
//取出客户端传入的所有参数名
ArrayList<String> params =
new ArrayList<String>();
String url=
null
;
while (keys.hasMoreElements()){
String key = (String) keys.nextElement();
/**
* 如果请求参数内有如下几种表示,这此参数不参与远程请求
*/
if
(key.equals(
"requesturl"
)){
//判断参数是否是,远程请求地址
url = req.getParameter(key);
}
else if
(key.equals(
"typedata"
)){
//判断请求数据类型,暂时没有用到
}
else if
(key.equals(
"returntype"
)){
//判断请求返回类型,暂时没有用到
}
else
{
params.add(key);
//其它加入参数列表,此处为参与远程请求的参数
requestType =
true
;
//修改标记,表求远程请求为POST方式
}
}
HttpClient client =
new HttpClient();
HttpMethod method =
null
;
if
(requestType){
//判断请求方式,并实例化HttpMethod对象,true:POST,false:GET
method =
new UTF8PostMethod(url);
for
(String name : params){
//迭代POST参数,加入到请求中
String _value = req.getParameter(name);
((PostMethod)method).setParameter(name,_value);
}
}
else
{
method =
new GetMethod(url);
}
client.executeMethod(method);
//执行请求
String bodystr = method.getResponseBodyAsString();
//返回结果
out.println(bodystr);
//将结果返回给客户端
}
/**
* 内部类,转换URL字符串为UTF-8
* @author Administrator
*
*/
private static class UTF8PostMethod
extends PostMethod {
public UTF8PostMethod(String url) {
super
(url);
}
@Override
public String getRequestCharSet() {
return "UTF-8"
;
}
}
}
|
Clever use of jquery and servlets for cross-domain requests