jsonp获取服务器数据的方式
- 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获取服务器数据的方式的更多相关文章
- XMLHTTPRequestObject获取服务器数据
http://www.educity.cn/develop/526316.html 在Web客户端使用xmlhttp对象,可以十分方便的和服务器交换数据,我们可以获取和发送任何类型的数据,甚至二进制数 ...
- SpringMVC 02: SpringMVC响应get和post请求 + 5种获取前端数据的方式
响应get和post请求 SpringMVC中使用@RequestMapping注解完成对get请求和post请求的响应 项目结构和配置文件与SpringMVC博客集中的"SpringMVC ...
- nodejs获取服务器数据到页面
const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const route ...
- 十一、React 获取服务器数据: axios插件、 fetch-jsonp插件的使用
react获取服务器APi接口的数据: react中没有提供专门的请求数据的模块.但是我们可以使用任何第三方请求数据模块实现请求数据 一.axios 获取Api数据 使用文档:https://www. ...
- 模拟获取post数据的方式
使用下面两种方法可以获取post数据 .通过$HTTP_RAW_POST_DATA获取 $post=$GLOBALS['HTTP_RAW_POST_DATA']; 但需要修改相应的php.ini指令 ...
- IOS开发之Post 方式获取服务器数据
//1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...
- AJAX 跨域请求 - JSONP获取JSON数据
Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...
- AJAX 跨域请求的解决办法:使用 JSONP获取JSON数据
由于受到浏览器的限制,ajax不允许跨域通信.如果尝试从不同的域请求数据,会出现安全错误.如果能控制数据驻留的远程服务器并且每个请求都前往同一域,就可以避免这些安全错误.但是,如果仅停留在自己的服务器 ...
- 【转】AJAX 跨域请求 - JSONP获取JSON数据
来源:http://justcoding.iteye.com/blog/1366102/ Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流 ...
随机推荐
- 使用NPOI和线程池快速加载EXCEL数据
private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cs ...
- frequentism-and-bayesianism-chs-iii
frequentism-and-bayesianism-chs-iii 频率主义 vs 贝叶斯主义 III:置信(Confidence)与可信(Credibility),频率主义与科学,不能混为一 ...
- 研究AVCaptureDevice
一.Apple Resource 1. wwdc 2014: Camera Caputre: Manual Controls 2. Exaple code: AVCam&AVCamManul ...
- 2013年优秀jQuery插件
今天为大家推荐的是2013年的第一期,在这期里面十个jQuery插件涵盖了响应式的网格布局插件.图片放大插件.表单元素中自定义select插件,google 地图插件.文件拖放上传插件.tooltip ...
- 流程控制语句和增强for循环
import java.lang.Math; //import java.util.Arrays; public class test{ public static void main(String[ ...
- MySQL各个版本区别
MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...
- 【动态规划】 之最长公共子序列LCS
int lcs_len(char *a, char *b, int c[][N]){ int aLen=strlen(a), bLen=strlen(b), i,j; ; i<=aLen; i+ ...
- Thread类详解
java.lang 类 Thread java.lang.Object java.lang.Thread 所有已实现的接口: Runnable public class Threadextends O ...
- 深入浅出ES6(九):学习Babel和Broccoli,马上就用ES6
作者 Jason Orendorff github主页 https://github.com/jorendorff 现在,我们将向你分步展示如何做到的这一切.上面提及的工具被称为转译器,你可以将它 ...
- 用C#读取,写入ini文件
[DllImport("kernel32.dll")] private static extern bool WritePrivateProfileString(string se ...