• jsonp获取服务器的数据,有两种
  • 一,跨域
  • 二,不跨域
  • 如果跨域
  • js的写法有两种
  • 1,
  • <script type="text/javascript">
  • $(function() {
  • $.getJSON('http://localhost:8090/search?jsoncallback=?' ,
  • function(json) {
  • alert(json);
  • var html = "";
  • for (var key in json.data) {
  • html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";
  • html = html + "名称:" + json.data[key].filename + "<br>";
  • html = html + "category:" + json.data[key].category + "<br>";
  • html = html + "height:" + json.data[key].height + "<br>";
  • html = html + "width:" + json.data[key].width + "<br>";
  • html = html + "length:" + json.data[key].length + "<br>";
  • html = html + "<hr>";
  • }
  • $('#imageList').html(html);
  • }
  • );
  • });
  • </script>
  • get的ur后面有jsoncallback=?
  • 这时候,要在服务器端增加如下代码
  • hr.getParameter("jsoncallback") +"({jsonp数据的格式})"
  • 例子
  • jQuery1510062266528242707175_1324369820794({"data":[{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f910a","keyword" : "","width" : "259","chunkSize" : "262144","length" : "9082","md5" : "534a94756fc98a6db0483ee702297a82","filename" : "img001_images_029.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"},{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f9108","keyword" : "","width" : "259","chunkSize" : "262144","length" : "10987","md5" : "7b85b894132f78b2b90cd3fef27317a6","filename" : "img001_images_028.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"}]});
  • 注意,是如下格式jQuery1510062266528242707175_1324369820794({xxxx}),要加个()的哦。
  • 2,get的ur后面有jsoncallback=?的写法可以改为get的ur后面有jsoncallback=getdata
  • 同时   function(json) 前要加上getdata=
  • 具体如下
  • <script type="text/javascript">
  • $(function() {
  • $.getJSON('http://localhost:8090/search?jsoncallback=getdata' ,
  • getdata=function(json) {
  • alert(json);
  • var html = "";
  • for (var key in json.data) {
  • html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";
  • html = html + "名称:" + json.data[key].filename + "<br>";
  • html = html + "category:" + json.data[key].category + "<br>";
  • html = html + "height:" + json.data[key].height + "<br>";
  • html = html + "width:" + json.data[key].width + "<br>";
  • html = html + "length:" + json.data[key].length + "<br>";
  • html = html + "<hr>";
  • }
  • $('#imageList').html(html);
  • }
  • );
  • });
  • </script>
  • 这个时候,服务器端就不需要加上hr.getParameter("jsoncallback")了,直接返回json格式就行了。
  • 具体参见
  • http://www.cnblogs.com/5201314/archive/2009/06/23/1509552.html
  • http://51mst.iteye.com/blog/1170798
  • 二,如果是不跨域的,
  • 不需要加上jsoncallback,直接如下就行了,同时服务器返回的格式,就是纯jsonp的格式。
  • <script type="text/javascript">
  • $(function() {
  • $.getJSON('http://localhost:8090/search' ,
  • function(json) {
  • alert(1);
  • var html = "";
  • for (var key in json.data) {
  • html = html + "<img src='http://localhost:8090/img/99999/o/" + json.data[key].filename + "'><br>";
  • html = html + "名称:" + json.data[key].filename + "<br>";
  • html = html + "category:" + json.data[key].category + "<br>";
  • html = html + "height:" + json.data[key].height + "<br>";
  • html = html + "width:" + json.data[key].width + "<br>";
  • html = html + "length:" + json.data[key].length + "<br>";
  • html = html + "<hr>";
  • }
  • $('#imageList').html(html);
  • }
  • );
  • });
  • </script>
  • 服务器返回代码
  • {"data":[{"category" : "all","height" : "194","_id" : "4ebce7b5523e7e91029f910a","keyword" : "","width" : "259","chunkSize" : "262144","length" : "9082","md5" : "534a94756fc98a6db0483ee702297a82","filename" : "img001_images_029.jpeg","contentType" : "null","uploadDate" : "Fri Nov 11 18:15:33 JST 2011","aliases" : "null"}]}

jsonp获取服务器数据的方式的更多相关文章

  1. XMLHTTPRequestObject获取服务器数据

    http://www.educity.cn/develop/526316.html 在Web客户端使用xmlhttp对象,可以十分方便的和服务器交换数据,我们可以获取和发送任何类型的数据,甚至二进制数 ...

  2. SpringMVC 02: SpringMVC响应get和post请求 + 5种获取前端数据的方式

    响应get和post请求 SpringMVC中使用@RequestMapping注解完成对get请求和post请求的响应 项目结构和配置文件与SpringMVC博客集中的"SpringMVC ...

  3. nodejs获取服务器数据到页面

    const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const route ...

  4. 十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用

    react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数据模块实现请求数据 一.axios 获取Api数据 使用文档:https://www. ...

  5. 模拟获取post数据的方式

    使用下面两种方法可以获取post数据 .通过$HTTP_RAW_POST_DATA获取 $post=$GLOBALS['HTTP_RAW_POST_DATA']; 但需要修改相应的php.ini指令 ...

  6. IOS开发之Post 方式获取服务器数据

    //1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...

  7. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  8. AJAX 跨域请求的解决办法:使用 JSONP获取JSON数据

    由于受到浏览器的限制,ajax不允许跨域通信.如果尝试从不同的域请求数据,会出现安全错误.如果能控制数据驻留的远程服务器并且每个请求都前往同一域,就可以避免这些安全错误.但是,如果仅停留在自己的服务器 ...

  9. 【转】AJAX 跨域请求 - JSONP获取JSON数据

    来源:http://justcoding.iteye.com/blog/1366102/ Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流 ...

随机推荐

  1. MongoDB 基础

    1. 安装 mongodb-win32-x86_64-2008plus-2.6.12-signed.msi,下载地址 https://www.mongodb.com/download-center#c ...

  2. PHP 路径或URL操作

    echo 'documentroot:'.$_SERVER['DOCUMENT_ROOT'].'<br>'; //根目录,在apache的配置文件里定义:httpd.conf 比如:Doc ...

  3. cts 测试环境安装 ubuntu

    1 下载cts测试包 和 sdk 包 http://source.android.com/compatibility/downloads.html  ----cts 包 http://develope ...

  4. BZOJ 1029 [JSOI2007] 建筑抢修(贪心)

    1029: [JSOI2007]建筑抢修 Time Limit: 4 Sec  Memory Limit: 162 MBSubmit: 2285  Solved: 1004[Submit][Statu ...

  5. TJU 4087. box

    题目:Tuhao and his two small partners participated in the tournament.But in the end, they lost the cha ...

  6. jquery的show/hide性能测试

    这篇文章是jQuery各种 show/hide方式的性能测试.作者之所以测试这个源于Robert Duffy在SanFrancisco举行的jQuery大会上的一句话:“.hide()和.show() ...

  7. 报名|「OneAPM x DaoCloud」技术公开课:Docker性能监控!

    如今,越来越多的公司开始 Docker 了,「三分之二的公司在尝试了 Docker 后最终使用了它」,也就是说 Docker 的转化率达到了 67%,同时转化时长也控制在 60 天内. 既然 Dock ...

  8. 转载:PHP,MySQL的安装与配置

    本文转自:http://www.cnblogs.com/janas/archive/2012/08/27/2659240.html 一.安装配置PHP 1.下载Php的版本zip包之后,解压缩到指定目 ...

  9. C#实现文件增量备份

    最近将客户的一个ASP网站部署到了公司的机房云服务器上,该ASP网站的文件总容量已有将近4GB. 虽然现在硬盘容量很大,但每天一次完整备份的话,那占用的硬盘空间会急剧上升,考虑一个更优的备份方案就是每 ...

  10. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...