jquery getJson
JSON是一种理想的数据传输格式,它能够很好的融合与javascript或其他宿主语言,并且可以被JS直接使用。使用JSON相比传统的通过 GET、POST直接发送“裸体”数据,在结构上更为合理,也更为安全。至于jquery的getJSON()函数,只是设置了JSON参数的 ajax()函数的一个简化版本。这个函数也是可以跨域使用的,相比get()、post()有一定优势。另外这个函数可以通过把请求url写 成”myurl?callback=X”这种格式,让程序执行回调函数X。
getJSON(url,[data],[callback]) url (String) 发送请求地址 data (Map) (可选) 待发送 Key/value 参数 callback (Function) (可选) 载入成功时回调函数。
这是一个快速的AJax处理函数,相当于:
$.ajax({ url: url, dataType: 'json', data: data, success: callback });
数据发送到服务器是作为一个附加到查询字符串的URL。如果该值的data
参数是一个对象,它会转换为一个字符串和URL编码,然后才追加到URL。
大多数实现将指定一个成功的处理程序:
$.getJSON('ajax/test.json', function(data) { var items = []; $.each(data, function(key, val) { items.push('<li id="' + key + '">' + val + '</li>'); }); $('<ul/>', { 'class': 'my-new-list', html: items.join('') }).appendTo('body'); });
这个例子,当然遵循JSON文件的结构:
{ "one": "Singular sensation", "two": "Beady little eyes", "three": "Little birds pitch by my doorstep" }
使用这种结构,这个例子遍历请求的数据,建立了一个无序列表,并追加到“body”。
在jQuery 1.5的约定接口也使jQuery的Ajax方法,其中包括$.getJSON()
,以链多个.success()
, .complete()
和.error()
回调的一个请求,甚至回调后分配这些请求可能已经完成。如果请求已经完成,立即触发回调。
// Assign handlers immediately after making the request, // and remember the jqxhr object for this request var jqxhr = $.getJSON("example.json", function() { alert("success"); }) .success(function() { alert("second success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); }); // perform other work here ... // Set another completion function for the request above jqxhr.complete(function(){ alert("second complete"); });
同一起源的政策 ;该请求不能成功地检索来自不同的域,子域或协议的数据。
演示 演示2 对象演示 数组演示
js加载这个JSON数据,并使用返回的JSON数据中的 name值:
$.getJSON("test.js", function(json) {
alert("JSON Data: " + json.users[3].name);
});
html
http://my.oschina.net/duwaiweb/blog/80576
http://rain-2372.iteye.com/blog/1124765
http://blog.csdn.net/huileiforever/article/details/12163385
http://stackoverflow.com/questions/17226030/use-each-and-if-with-getjson
http://www.w3school.com.cn/jquery/ajax_getjson.asp
您可能感兴趣的文章:
$.getJSON在IE下失效的原因分析及解决方法
jQuery的3种请求方式$.post,$.get,$.getJSON
ie下jquery.getJSON的缓存问题的处理方法
Jquery getJSON方法详细分析
Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
jquery中ajax函数执行顺序问题之如何设置同步
Jquery中getJSON在asp.net中的使用说明
jquery getJSON跨域调用数据的例子
通过jquery的$.getJSON做一个跨域ajax请求试验
JQuery中的$.getJSON 使用说明
上一篇:jQuery九宫格图片拖放
下一篇:《锋利的jQuery》学习笔记
[关闭]
js加载这个JSON数据,并使用返回的JSON数据中的 name值:
$.getJSON("test.js", function(json) {
alert("JSON Data: " + json.users[3].name);
});
html
http://my.oschina.net/duwaiweb/blog/80576
http://rain-2372.iteye.com/blog/1124765
http://blog.csdn.net/huileiforever/article/details/12163385
http://stackoverflow.com/questions/17226030/use-each-and-if-with-getjson
http://www.w3school.com.cn/jquery/ajax_getjson.asp
您可能感兴趣的文章:
$.getJSON在IE下失效的原因分析及解决方法
jQuery的3种请求方式$.post,$.get,$.getJSON
ie下jquery.getJSON的缓存问题的处理方法
Jquery getJSON方法详细分析
Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
jquery中ajax函数执行顺序问题之如何设置同步
Jquery中getJSON在asp.net中的使用说明
jquery getJSON跨域调用数据的例子
通过jquery的$.getJSON做一个跨域ajax请求试验
JQuery中的$.getJSON 使用说明