跨域获取json字符串
跨域出现的错误:
户代理: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E) 时间戳: Tue, 14 Feb 2012 14:58:44 UTC
新建
exp.jsp页面如下:
<%@ page contentType="text/html; charset=utf-8" language="java"%> <%@ page import="java.util.*,java.lang.*,java.io.*,java.net.*"%> <%!private String getContentByUrl(String urlstring) { StringBuffer document = new StringBuffer(); try { URL url = new URL(urlstring); URLConnection conn = url.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { document.append(line + "\n"); } reader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return document.toString(); } %> <%=getContentByUrl(http://exp.php?callback=xxx)%>
index.jsp页面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> <script> function init(){ jQuery.ajax({ type : 'GET', url : 'exp.jsp', success : function(data){ alert(data); } }); } </script> </head> <body> <button onclick="init()" >点击我就跨域给你看</button> </body> </html>
界面运行效果如小:
posted on 2012-02-15 01:32 阳光总在风雨后001 阅读(808) 评论(0) 编辑 收藏 举报