Jquery getJSON 实现跨域请求 --- callback
2014-10-23 11:42
555 查看
Jquery getJSON 实现跨域请求 --- callback
当前域名访问 me.com 文件
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script>
$.getJSON('http://localhost/test.php?callback=?',{p1:'11'},function(msg){
//调用域localhost的test.php(其实两个文件在相同目录下) console.dir(msg);})
</script>
localhost/test.php 文件
<?php die($_GET['callback'].'('.json_encode(array('msg'=>1)).')');?>
getJSON 核心思想是 执行getJSON 注册一个以当前时间命名的function 然后再服务端执行该函数 (具体源码)
1、
getJSON: function( url, data, callback ) { return jQuery.get(url, data, callback, "json"); },
2、
get: function( url, data, callback, type ) { // shift
arguments if data argument was ommited if ( jQuery.isFunction( data ) ) { callback = data; data = null;
} return jQuery.ajax({ type: "GET", url: url, data: data, success: callback, dataType: type });},
3、
// Build temporary JSONP function if ( s.dataType == "json" &&
(s.data && s.data.match(jsre) || s.url.match(jsre)) ) { jsonp = "jsonp" + jsc++; // Replace
the =? sequence both in the query string and the data if ( s.data ) s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
s.url = s.url.replace(jsre, "=" + jsonp + "$1"); // We need to make sure // that
a JSONP style response is executed properly s.dataType = "script"; // Handle JSONP-style
loadingwindow[ jsonp ] = function(tmp){ data = tmp; success(); complete(); // Garbage
collect window[ jsonp ] = undefined;try{ delete window[ jsonp ]; } catch(e){} if (
head ) head.removeChild( script ); }; }
当前域名访问 me.com 文件
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script><script>
$.getJSON('http://localhost/test.php?callback=?',{p1:'11'},function(msg){
//调用域localhost的test.php(其实两个文件在相同目录下) console.dir(msg);})
</script>
localhost/test.php 文件
<?php die($_GET['callback'].'('.json_encode(array('msg'=>1)).')');?>
getJSON 核心思想是 执行getJSON 注册一个以当前时间命名的function 然后再服务端执行该函数 (具体源码)
1、
getJSON: function( url, data, callback ) { return jQuery.get(url, data, callback, "json"); },
2、
get: function( url, data, callback, type ) { // shift
arguments if data argument was ommited if ( jQuery.isFunction( data ) ) { callback = data; data = null;
} return jQuery.ajax({ type: "GET", url: url, data: data, success: callback, dataType: type });},
3、
// Build temporary JSONP function if ( s.dataType == "json" &&
(s.data && s.data.match(jsre) || s.url.match(jsre)) ) { jsonp = "jsonp" + jsc++; // Replace
the =? sequence both in the query string and the data if ( s.data ) s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
s.url = s.url.replace(jsre, "=" + jsonp + "$1"); // We need to make sure // that
a JSONP style response is executed properly s.dataType = "script"; // Handle JSONP-style
loadingwindow[ jsonp ] = function(tmp){ data = tmp; success(); complete(); // Garbage
collect window[ jsonp ] = undefined;try{ delete window[ jsonp ]; } catch(e){} if (
head ) head.removeChild( script ); }; }
相关文章推荐
- Jquery getJSON 实现跨域请求 --- callback
- 使用Jquery的getJSON跨域请求蘑菇街图片资源实现瀑布流
- jquery getJSON跨域请求
- jQueryAjax请求-jQuery.getJSON(url, [data], [callback])
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 用jquery.getJSON实现跨域提交
- 用jQuery.getJSON()跨域请求JSON数据
- jquery $.getJSON()跨域请求
- jquery的ajax和getJson跨域获取json数据的实现方法
- jquery的ajax和getJson跨域获取json数据的实现方法
- jquery使用$.getJson()跨域大数据量请求方法
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 利用JQuery jsonp实现Ajax跨域请求 .Net 的*.handler 和 WebService,返回json数据
- 使用Jquery中getJSON方法实现跨域
- 使用$.getJSON实现跨域ajax请求示例代码
- jquery使用$.getJson()跨域大数据量请求方法
- 使用Jquery中getJSON和getScript方法实现跨域
- 使用$.getJSON实现跨域ajax请求
- 通过jquery的$.getJSON自己做一个跨域ajax请求试验