关于浏览器跨域,在web开发中经常遇到,单纯的js和Ajax要做到跨域,很不容易,当前解决跨域问题,无外乎Iframe等几种方式,其实最通用最简单的做法就是通过后台来访问页面,得到数据或者标记,从而你可以进行进一步的处理,以java举例:
通过访问具体的页面直接得到页面的内容,你可以直接将json类型的数据写在页面,这种是最直接而且有效的方式。
public String analysisStream(string url) throws Exception
{
// 测试远程访问页面来获取html页面的源代码
URL url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(false);
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取html源代码
InputStream reader = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
byte[] buff = new byte[1024];
int size = 0;
while ((size = reader.read(buff)) != -1)
{
buffer.append(new String(buff, 0, size));
}
return buffer.toString();
}