• 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. Metasploit Framework命令汇总

    一.msfconsole ? 帮助菜单back 从当前环境返回banner 显示一个MSF bannercd 切换目录color 颜色转换connect 连接一个主机exit 退出MSFhelp 帮助 ...

  2. tcp 多线程与多进程调用close

    http://blog.csdn.net/russell_tao/article/details/13092727 大家知道,所谓线程其实就是“轻量级”的进程.创建进程只能是一个进程(父进程)创建另一 ...

  3. 如何创建和发布.asmx Web Service

    创建和发布Web ServiceWeb服务方法中可以返回一个DataSet对象 WEB服务可以说是下一代WEB应用程序的基础,无论客户端是WINDOWS应用.ASP.NET Web Form程序.甚至 ...

  4. centos mysql 操作

    安装mysqlyum -y install mysql-server 修改mysql配置 vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记 暂时修改一下编码(添加在密码下 ...

  5. xxx.properties获取方法

    1.手动获取加载 Locale locale = Locale.getDefault(); ResourceBundle localResource = ResourceBundle.getBundl ...

  6. Xmarks丢失书签

    想体验下Xmarks,不同浏览器同步书签,听说很好用,就安装Chrome插件,没想到竟然把我的所有书签都丢了. 不过在网上找到了回复的办法,也很简单: 原始地址:http://irising.me/2 ...

  7. HDU3507 Print Article(斜率优化dp)

    前几天做多校,知道了这世界上存在dp的优化这样的说法,了解了四边形优化dp,所以今天顺带做一道典型的斜率优化,在百度打斜率优化dp,首先弹出来的就是下面这个网址:http://www.cnblogs. ...

  8. iOS视频压缩

    // // ViewController.m // iOS视频测试 // // Created by apple on 15/8/19. // Copyright (c) 2015年 tqh. All ...

  9. Javascript 正则表达式笔记

    \d 元字符 + 量词 \w 常用的字符a-zA-Z0-9 .除回车之外的字符 ?0-1个字符 量词 只有前面是元字符,才变现量词 * 0-n 量词 /^\d+$/ 以字符开头,义字符结尾 [0-9] ...

  10. 使用时间戳引入css、js文件

    前言 最近在一家创业公司实习,主要负责新版官网和商家平台管理系统的前端开发和维护,每次测试都要上传文件到ftp服务器端测试,初期由于更新修改比较频繁,每次都是直接上传覆盖css.js.php文件,链接 ...