?
This document uses PHP Chinese website manual Release
通過 HTTP GET 請求載入 JSON 數(shù)據(jù)。
在 jQuery 1.2 中,您可以通過使用JSONP形式的回調(diào)函數(shù)來加載其他網(wǎng)域的JSON數(shù)據(jù),如 "myurl?callback=?"。jQuery 將自動替換 ? 為正確的函數(shù)名,以執(zhí)行回調(diào)函數(shù)。 注意:此行以后的代碼將在這個回調(diào)函數(shù)執(zhí)行前執(zhí)行。
url:發(fā)送請求地址。
data:待發(fā)送 Key/value 參數(shù)。
callback:載入成功時回調(diào)函數(shù)。
從 Flickr JSONP API 載入 4 張最新的關(guān)于貓的圖片。
<div id="images"></div>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format =json&jsoncallback=?", function(data){ $.each(data.items, function(i,item){ $("<img/>").attr("src", item.media.m).appendTo("#images"); if ( i == 3 ) return false; }); });
從 test.js 載入 JSON 數(shù)據(jù)并顯示 JSON 數(shù)據(jù)中一個 name 字段數(shù)據(jù)。
$.getJSON("test.js", function(json){ alert("JSON Data: " + json.users[3].name); });
從 test.js 載入 JSON 數(shù)據(jù),附加參數(shù),顯示 JSON 數(shù)據(jù)中一個 name 字段數(shù)據(jù)。
$.getJSON("test.js", { name: "John", time: "2pm" }, function(json){ alert("JSON Data: " + json.users[3].name); });