jquery.getJson()跨域
由于浏览器的同源特性限制(Same_origin_policy),这样js去访问非本域下的资源,使用平常的做法就不会实现了。
要想完成跨域访问资源传统上有三个方法:
写道
The browser security model dictates that XMLHttpRequest, frames, etc. must have the same domain in order to communicate. That's not a terrible idea, for security reasons, but it sure does make distributed (service oriented, mash-up, whatever it's called this week) web development suck.
There are traditionally three solutions to solving this problem.
Local proxy:
Needs infrastructure (can't run a serverless client) and you get double-taxed on bandwidth and latency (remote - proxy - client).
Flash:
Remote host needs to deploy a crossdomain.xml file, Flash is relatively proprietary and opaque to use, requires learning a one-off moving target programming langage.
Script tag:
Difficult to know when the content is available, no standard methodology, can be considered a "security risk".
还有另外一种实现方法就是jsonp(json + padding )
写道
I'm proposing a new technology agnostic standard methodology for the script tag method for cross-domain data fetching: JSON with Padding, or simply JSONP.
jQuery中的getJson就是使用这样的方法实现的,当然$.ajax方法也可以完成,这是jQuery的ajax最终高调用方法
使用getJson完成跨域操作需要客户端与服务器端两部分共同协作
客户端:js
写道
JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.
即在getJson的url参数末尾加个callback=?这样的参数
$.getJson('http://other-domain.com/get_json_data.htm?callback=?', function(data) {
alert(data);
});
服务器:java(struts2)
在正常的ajax返回数据的参数配置上多添加一个参数
callbackParameter,同时指定客户端传递过来的参数callback
<result type=”json”>
<param name=”callbackParameter”>callback</param>
</result>
原理是,在客户端类似生成一个<script type=“text/javascript”>callback(data);</script>这样一个在客户端执行的一个js代码片段。
[1]http://api.jquery.com/jQuery.getJSON/
[2]http://api.jquery.com/jQuery.ajax/
[3]http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
[4]http://www.iteye.com/problems/45157
[5]http://www.iteye.com/problems/47527
[6]http://www.yindaoxian.com/html/web/Javascript_Ajax-13610.html
[7]http://en.wikipedia.org/wiki/Same_origin_policy
[8]http://lyzdz.com/index.php/archives/52
分享到:
相关推荐
jquery.json-2.3.min.js和jquery.json-2.3.js jQuery为开发插件提拱了两个方法,分别是:jQuery.extend(object); 为扩展jQuery类本身 jQuery.fn.extend(object);给jQuery对象添加方法。
在官网上一直下载不下来 然后共享在这 jquery.json-2.3.min.js和jquery.json-2.3.js
jquery.json2xml.js&&jquery.xml2json.js在jQuery的基础上实现json与xml的相互转换
jquery.jsoncookie.js
$(function(){ $.getJSON(url,function(data){ alert (data.Name); }) }); 服务器返回字符串: {“Name”:”loogn”,”Age”:23} 2,不同域名下 js: 代码如下: var url=”...
jquery.json-viewer.js是一款查看json格式数据的jquery插件。它可以将混乱的json数据漂亮的方式展示在页面中,并支持节点的伸展和收缩和语法高亮等功能。
jquery.json-viewer, 用于显示JSON数据的jQuery插件 jQuery浏览器JSON浏览器是一个jQuery插件,它可以通过将JSON对象转化为HTML来轻松显示JSON对象。功能:语法高亮显示在单击时折叠和展开子节点可以点击链接易于...
详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
jquery.json.min.2.3.0.js;jquery.ui.core.js;jquery-1.4.2.min.js;jquery.ui.datepicker.js;json2.js
Jquery-json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换。可序列化 JavaScript 对象、数值、字符串和数组到 JSON 字符串,同时可转换 JSON 字符串到 JavaScript。 var thing = {plugin: '...
jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery插件版 treeview1.0jsontree (jquery.treeview.js) jQuery...
jquery.json.min.js
jquery.json.js 2.4 jquery json库
jquery.json-2.4解析json对象
jquery提供的json插件,提供js对象与json字符串间互相转换
主要介绍了JQuery用$.ajax或$.getJSON跨域获取JSON数据的实现代码,需要的朋友可以参考下
jquery.json-2.2.