利用jsonp实现http 的跨域访问
2013-06-06 14:17
393 查看
弄了半天终于弄明白了。
目前我实现的是html的客户端页面 http访问 python的服务端。
服务端: (网上例子java servlet)
清单 7. 用 Java servlet 实现的 JSONP 服务
http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/
http请求的服务端 只要返回的是上面红色高亮部分就行了
这个也是可以:
/article/5315908.html
客户端:
html页面
$.getJSON("http://localhost:10085/show?appid=34567890&&callback=?", onDataReceived);
或者
或
$.ajax({
93 url: 'http://localhost:10085/show?callback=?',
94 method: 'GET',
95 dataType: 'jsonp',
96 data: setQueryString(),
97 success: onDataReceived
98 });
目前我实现的是html的客户端页面 http访问 python的服务端。
服务端: (网上例子java servlet)
清单 7. 用 Java servlet 实现的 JSONP 服务
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String jsonData = getDataAsJson(req.getParameter("symbol")); String output = req.getParameter("callback") + "(" + jsonData + ");"; resp.setContentType("text/javascript"); PrintWriter out = resp.getWriter(); out.println(output); // prints: jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"}); } |
http请求的服务端 只要返回的是上面红色高亮部分就行了
这个也是可以:
1 public class MyService : IHttpHandler 2 { 3 public void ProcessRequest(HttpContext context) 4 { 5 //获取回调函数名 6 string callback = context.Request.QueryString["callback"]; 7 //json数据 8 string json = "{\"name\":\"chopper\",\"sex\":\"man\"}"; 9 10 context.Response.ContentType = "application/json"; 11 //输出:回调函数名(json数据) 12 context.Response.Write(callback + "(" + json + ")"); 13 } 14 15 public bool IsReusable 16 { 17 get 18 { 19 return false; 20 } 21 } 22 }
/article/5315908.html
客户端:
html页面
$.getJSON("http://localhost:10085/show?appid=34567890&&callback=?", onDataReceived);
或者
$.getJSON("http://localhost:20002/MyService.ashx?callback=?",function(data){ alert(data.name + " is a a" + data.sex); });
或
$.ajax({
93 url: 'http://localhost:10085/show?callback=?',
94 method: 'GET',
95 dataType: 'jsonp',
96 data: setQueryString(),
97 success: onDataReceived
98 });
相关文章推荐
- jquery下利用jsonp跨域访问实现方法
- jquery下利用jsonp跨域访问实现方法
- angularJS1笔记-(18)-$http及用angular实现JSONP跨域访问过程
- 利用jsonp跨域调用百度js实现搜索框智能提示
- WCF 自托管、无配置文件实现jsonp(跨域)的访问
- 在ASP.NET MVC3 中利用Jsonp跨域访问
- 使用jsonp实现跨域访问的代码演示
- angular 使用$http.jsonp进行跨域访问以及修改目标JSON
- 利用Jsonp实现跨域请求,spring MVC+JQuery
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用AFNetworking 的AFHTTPSessionManager代码实现代理的方式访问任意指定IP地址,解决域名DNS上的问题
- 通过设置P3P头来实现跨域访问COOKIE_老王的技术手册 ( 我的新博客:http://huoding.com )_百度空间
- 利用jquery 的jsonp 进行跨域访问
- 前台实现跨域访问JSONP
- 利用CORS配置实现jQuery对WebApi及MVC的跨域访问
- 用jsonp实现跨域访问
- (转)在ASP.NET MVC3 中利用Jsonp跨域访问
- XMLHttpRequest实现跨域名访问RSS源
- springmvc实现jsonp跨域接口访问