利用JQuery和Servlet实现跨域提交请求示例分享_jquery
程序员文章站
2022-05-26 22:15:22
...
原理:JavaScript的Ajax不可以跨域,但是可以通过向本地的一个Servlet发出请求,由Servlet完成跨域。再把远程的结构返回给客户端。这样Ajax就可以跨域了。在后面,再发一个PHP版本的,请大家关注啊。下面是代码
//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");
}
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 params = new ArrayList();
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";
}
}
}
JS代码:
注意:在Post方式时,param1和param2为向远程发送的参数值,可以有多个。
复制代码 代码如下:
//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");
}
/**
* 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代码:
复制代码 代码如下:
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 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";
}
}
}
上一篇: ajax有关问题。在线结贴
推荐阅读