我试图通过在jsp中编写一些代理代码,从我的计算机本地对其他域进行ajax调用。这是我调用proxy.jsp页面的jQuery AJAX代码。
var search_metadata = 'https://doc.xyz.com/rest-services/services/get?id=';
var on_show_info = function() {
//alert("aa");
var outOfDomainCall = search_metadata + current_doc.id;//An XML document
alert(outOfDomainCall);
request_meta_info = $.ajax({
url: "proxy.jsp?url=" + outOfDomainCall,
type: 'GET',
success: on_get_metadata,
error: on_get_metadata_error
});
};
我的proxy.jsp文件是: -
<%@ page language="java" import="org.w3c.dom.*,javax.xml.parsers.DocumentBuilder,javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String urlToQuery = request.getParameter("url");
System.out.println("rj " +urlToQuery);
URL url = new URL(urlToQuery);
try {
url.openConnection();
}
catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String InputLine = null ;
while ((InputLine = in.readLine()) != null)
out.println( InputLine );
in.close();
//NodeList nl= doc.getElementsByTagName("attr");
%>
返回的XMl文件是: -
<results><result><attr value="HW/SW" name="Block.ProductLine"/><attr value="IC,REEL" name="Block.Description"/><attr value="CD90" name="Block.Number"/><attr value="ASIC_IC" name="Block.DocumentType"/><attr value="" name="Block.RevIncorpDate"/><attr value="" name="Block.Category"/><attr value="" name="Block.RevReleaseDate"/><attr value="Introductory" name="Block.rev"/></result></results>
那么如何使用proxy.jsp获取整个url内容并将整个内容作为响应发送回jQUery AJAX调用。每个链接都会生成一个XML文档。我尝试在proxy.jsp中做到这一点。让我知道如果我在proxy.jsp中做了什么错误,那么最好的方法是什么。我得到了这个回复,因为302发现: -
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>302 Found</title></head><body><h1>Found</h1><p>The document has moved <a href="HTTP://doc.xyz.com/rest-services/services/getmeta?id=CD90">here</a>.</p></body></html>
上面显示的url是我试图在proxy.jsp中调用的实际url然后为什么我得到这个302 Found错误。任何建议将不胜感激..