jQuery的getJSON为什么能跨域?
1. 因为浏览器的<script src='cross_domain.js' type='text/javascript' ></script>标记本身就是可跨域的。
2. 修改jsoncallback=?中的问号为指定的方法,此方法可匿名给出,jQuery会负责管理。
3. 二getJSON调用时,动态在<head>区域加入一个<script>并设置其src指向参数中给出的地址。
4. 数据端按定义给出JSON数据,并加入对应的jsoncallback方法,产生一个回调事件。
5. 回调这后,jQuery负责清理现场,去掉所添加的<script>标记。
下面是个调用救命示例:
var url = 'http://api.flickr.com/services/Feeds/photos_public.gne?tags=cat&tagmode=any&&format=json&jsoncallback=?';
$.getJSON(url,
function(data){
for(var i in data) alert(i+'='+data[i]);
}
);
在MF监测的过程中可以发现jQuery.getJSON会加入类似如下标记:
<script src="http://api.flickr.com/services/Feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=jsonp1271746675568&_=1271746724135"></script>
在flickr这个页面上的默认jsoncallback是jsonFlickrFeed。
返回的内容如:
jsonFlickrFeed({ "title": "Recent Uploads tagged cat","link": "http://www.flickr.com/photos/tags/cat/","description": "","modified": "2010-04-20T04:58:32Z","generator": "http://www.flickr.com/","items": [ { "title": "ragdoll","link": "http://www.flickr.com/photos/pearled/4536545813/","media": {"m":"http://farm5.static.flickr.com/4034/4536545813_291afee211_m.jpg"},"date_taken": "2010-04-20T14:58:32-08:00","description": " <p><a href=/"http://www.flickr.com/people/pearled//">pearled<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/pearled/4536545813//" title=/"ragdoll/"><img src=/"http://farm5.static.flickr.com/4034/4536545813_291afee211_m.jpg/" width=/"240/" height=/"160/" alt=/"ragdoll/" /><//a><//p> <p>82:365<//p>","published": "2010-04-20T04:58:32Z","author": "nobody@flickr.com (pearled)","author_id": "36207436@N08","tags": "cat 35mm whitecat ragdoll ragdollcat" },{ "title": "ancient lion","link": "http://www.flickr.com/photos/gansv1846/4530378014/","media": {"m":"http://farm5.static.flickr.com/4070/4530378014_b2305d8cc8_m.jpg"},"date_taken": "2010-04-17T12:23:09-08:00","description": " <p><a href=/"http://www.flickr.com/people/gansv1846//">omoo<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/gansv1846/4530378014//" title=/"ancient lion/"><img src=/"http://farm5.static.flickr.com/4070/4530378014_b2305d8cc8_m.jpg/" width=/"180/" height=/"240/" alt=/"ancient lion/" /><//a><//p> ","published": "2010-04-20T05:10:32Z","author": "nobody@flickr.com (omoo)","author_id": "23431480@N00","tags": "newyorkcity sculpture art museum architecture cat feline manhattan lion friendly atrium metropolitan metropolitanmuseumofart antiquities 1009fifthavenue lionatrest templeoddendur" },{ "title": "The fun cat.","link": "http://www.flickr.com/photos/48103164@N03/4535910481/","media": {"m":"http://farm5.static.flickr.com/4056/4535910481_01828b412d_m.jpg"},"date_taken": "2010-04-22T07:34:06-08:00","description": " <p><a href=/"http://www.flickr.com/people/48103164@N03//">RAY OF SUN2010<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/48103164@N03/4535910481//" title=/"The fun cat./"><img src=/"http://farm5.static.flickr.com/4056/4535910481_01828b412d_m.jpg/" width=/"239/" height=/"240/" alt=/"The fun cat./" /><//a><//p> <p>حياكم<br /> ياني ضحكت ضحك من هالبس المنسم :))<br /> <a href=/"http://bighugelabs.com/onblack.PHP?id=4535910481/" rel=/"nofollow/">View On Black<//a><br /> <br /> <br /> أجاوبك من هنا <br /> <a href=/"http://www.formspring.me/raysun1/" rel=/"nofollow/">www.formspring.me/raysun1<//a><//p>","published": "2010-04-20T03:50:04Z","author": "nobody@flickr.com (RAY OF SUN2010)","author_id": "48103164@N03","tags": "cat flickr ray good" },{ "title": "2010-04-19 - 03992","link": "http://www.flickr.com/photos/brianschultze/4537008550/","media": {"m":"http://farm5.static.flickr.com/4009/4537008550_5ed1709c25_m.jpg"},"date_taken": "2010-04-19T10:25:58-08:00","description": " <p><a href=/"http://www.flickr.com/people/brianschultze//">Mojo Yugen<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/brianschultze/4537008550//" title=/"2010-04-19 - 03992/"><img src=/"http://farm5.static.flickr.com/4009/4537008550_5ed1709c25_m.jpg/" width=/"240/" height=/"160/" alt=/"2010-04-19 - 03992/" /><//a><//p> ","published": "2010-04-20T03:18:10Z","author": "nobody@flickr.com (Mojo Yugen)","author_id": "99001530@N00","tags": "pet storm cat gabby" },{ "title": "2010-04-19 - 03989","link": "http://www.flickr.com/photos/brianschultze/4536353357/","media": {"m":"http://farm5.static.flickr.com/4069/4536353357_93bb731e22_m.jpg"},"date_taken": "2010-04-19T10:25:50-08:00","description": " <p><a href=/"http://www.flickr.com/people/brianschultze//">Mojo Yugen<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/brianschultze/4536353357//" title=/"2010-04-19 - 03989/"><img src=/"http://farm5.static.flickr.com/4069/4536353357_93bb731e22_m.jpg/" width=/"240/" height=/"204/" alt=/"2010-04-19 - 03989/" /><//a><//p> ","published": "2010-04-20T03:09:07Z","tags": "pet cat gabby" },{ "title": "2010-04-19 - 03984","link": "http://www.flickr.com/photos/brianschultze/4536967130/","media": {"m":"http://farm3.static.flickr.com/2704/4536967130_bdcbc03e2f_m.jpg"},"date_taken": "2010-04-19T10:25:42-08:00","description": " <p><a href=/"http://www.flickr.com/people/brianschultze//">Mojo Yugen<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/brianschultze/4536967130//" title=/"2010-04-19 - 03984/"><img src=/"http://farm3.static.flickr.com/2704/4536967130_bdcbc03e2f_m.jpg/" width=/"240/" height=/"199/" alt=/"2010-04-19 - 03984/" /><//a><//p> ","published": "2010-04-20T03:02:04Z",{ "title": "2010-04-19 - 03980","link": "http://www.flickr.com/photos/brianschultze/4536939300/","media": {"m":"http://farm5.static.flickr.com/4060/4536939300_3b83138b62_m.jpg"},"date_taken": "2010-04-19T10:25:20-08:00","description": " <p><a href=/"http://www.flickr.com/people/brianschultze//">Mojo Yugen<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/brianschultze/4536939300//" title=/"2010-04-19 - 03980/"><img src=/"http://farm5.static.flickr.com/4060/4536939300_3b83138b62_m.jpg/" width=/"240/" height=/"160/" alt=/"2010-04-19 - 03980/" /><//a><//p> ","published": "2010-04-20T02:50:48Z",{ "title": "IMG_1179","link": "http://www.flickr.com/photos/sbierman/4536297183/","media": {"m":"http://farm5.static.flickr.com/4064/4536297183_c9ee8db2fc_m.jpg"},"date_taken": "2010-03-23T18:23:52-08:00","description": " <p><a href=/"http://www.flickr.com/people/sbierman//">Shawn Bierman<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/sbierman/4536297183//" title=/"IMG_1179/"><img src=/"http://farm5.static.flickr.com/4064/4536297183_c9ee8db2fc_m.jpg/" width=/"240/" height=/"160/" alt=/"IMG_1179/" /><//a><//p> ","published": "2010-04-20T02:47:28Z","author": "nobody@flickr.com (Shawn Bierman)","author_id": "55338475@N00","tags": "cute animal cat morninglight feline shelbyfarms" },{ "title": "Needle felted bengal kitten","link": "http://www.flickr.com/photos/26900025@N03/4536215163/","media": {"m":"http://farm5.static.flickr.com/4057/4536215163_581b82943d_m.jpg"},"date_taken": "2010-03-28T12:39:46-08:00","description": " <p><a href=/"http://www.flickr.com/people/26900025@N03//">emeyer1044<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/26900025@N03/4536215163//" title=/"Needle felted bengal kitten/"><img src=/"http://farm5.static.flickr.com/4057/4536215163_581b82943d_m.jpg/" width=/"240/" height=/"180/" alt=/"Needle felted bengal kitten/" /><//a><//p> ","published": "2010-04-20T02:14:47Z","author": "nobody@flickr.com (emeyer1044)","author_id": "26900025@N03","tags": "felted cat pig kitten felting ooak donkey needle" },{ "title": "Merlin the Maine Coon","link": "http://www.flickr.com/photos/kilkennycat/4536049009/","media": {"m":"http://farm5.static.flickr.com/4069/4536049009_7e9254aa9a_m.jpg"},"date_taken": "2010-04-18T18:26:46-08:00","description": " <p><a href=/"http://www.flickr.com/people/kilkennycat//">Kilkennycat<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/kilkennycat/4536049009//" title=/"Merlin the Maine Coon/"><img src=/"http://farm5.static.flickr.com/4069/4536049009_7e9254aa9a_m.jpg/" width=/"240/" height=/"189/" alt=/"Merlin the Maine Coon/" /><//a><//p> ","published": "2010-04-20T01:07:55Z","author": "nobody@flickr.com (Kilkennycat)","author_id": "94665864@N00","tags": "portrait cat photography kitty mainecoon" },{ "title": "Maine Coon","link": "http://www.flickr.com/photos/kilkennycat/4536049127/","media": {"m":"http://farm5.static.flickr.com/4063/4536049127_5e1f765165_m.jpg"},"date_taken": "2010-04-18T18:27:26-08:00","description": " <p><a href=/"http://www.flickr.com/people/kilkennycat//">Kilkennycat<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/kilkennycat/4536049127//" title=/"Maine Coon/"><img src=/"http://farm5.static.flickr.com/4063/4536049127_5e1f765165_m.jpg/" width=/"181/" height=/"240/" alt=/"Maine Coon/" /><//a><//p> ","published": "2010-04-20T01:07:58Z",{ "title": "Waiting,Watching","link": "http://www.flickr.com/photos/sousakuryoku/4535984397/","media": {"m":"http://farm3.static.flickr.com/2412/4535984397_78a1194059_m.jpg"},"date_taken": "2010-04-19T18:07:08-08:00","description": " <p><a href=/"http://www.flickr.com/people/sousakuryoku//">Lance Szydzik<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/sousakuryoku/4535984397//" title=/"Waiting,Watching/"><img src=/"http://farm3.static.flickr.com/2412/4535984397_78a1194059_m.jpg/" width=/"240/" height=/"160/" alt=/"Waiting,Watching/" /><//a><//p> <p>I let the cat in my room sometimes just because it means I get free well lit cat photos. And it/'s worth it.<//p>","published": "2010-04-20T00:39:04Z","author": "nobody@flickr.com (Lance Szydzik)","author_id": "22310636@N07","tags": "portrait pet window cat canon lens fur outdoors 50mm prime sitting dof bokeh sunny whiskers ii 18 t1i" },{ "title": "que pose","link": "http://www.flickr.com/photos/zyanya/4536556754/","media": {"m":"http://farm3.static.flickr.com/2775/4536556754_05000aafb6_m.jpg"},"date_taken": "2010-04-10T18:25:54-08:00","description": " <p><a href=/"http://www.flickr.com/people/zyanya//">rbrleon<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/zyanya/4536556754//" title=/"que pose/"><img src=/"http://farm3.static.flickr.com/2775/4536556754_05000aafb6_m.jpg/" width=/"240/" height=/"180/" alt=/"que pose/" /><//a><//p> <p>kyara y sus poses únicas<//p>","published": "2010-04-20T00:10:43Z","author": "nobody@flickr.com (rbrleon)","author_id": "87302075@N00","tags": "cat pose gato" },{ "title": "the Cheshire ","link": "http://www.flickr.com/photos/29456355@N08/4536496046/","media": {"m":"http://farm3.static.flickr.com/2744/4536496046_5ab29182c3_m.jpg"},"date_taken": "2010-02-16T02:21:44-08:00","description": " <p><a href=/"http://www.flickr.com/people/29456355@N08//">Shnoogins<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/29456355@N08/4536496046//" title=/"the Cheshire /"><img src=/"http://farm3.static.flickr.com/2744/4536496046_5ab29182c3_m.jpg/" width=/"240/" height=/"180/" alt=/"the Cheshire /" /><//a><//p> <p>inally caught my cat smiling on camera<//p>","published": "2010-04-19T23:42:27Z","author": "nobody@flickr.com (Shnoogins)","author_id": "29456355@N08","tags": "cat cheshire calico athena" },{ "title": "The Cheshire Calico","link": "http://www.flickr.com/photos/29456355@N08/4535857841/","media": {"m":"http://farm5.static.flickr.com/4019/4535857841_502404a2c4_m.jpg"},"date_taken": "2010-02-16T02:22:17-08:00","description": " <p><a href=/"http://www.flickr.com/people/29456355@N08//">Shnoogins<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/29456355@N08/4535857841//" title=/"The Cheshire Calico/"><img src=/"http://farm5.static.flickr.com/4019/4535857841_502404a2c4_m.jpg/" width=/"240/" height=/"180/" alt=/"The Cheshire Calico/" /><//a><//p> <p>finally caught my cat smiling on camera she usually does this face if shes smelled something stinky (which she enjoys) or what not.<//p>","published": "2010-04-19T23:40:42Z","tags": "smile cat cheshire calico athena" },{ "title": "Day 109 (April 19th): Marathon Monday","link": "http://www.flickr.com/photos/scousevet/4535820923/","media": {"m":"http://farm5.static.flickr.com/4009/4535820923_f972814468_m.jpg"},"date_taken": "2010-04-19T19:09:19-08:00","description": " <p><a href=/"http://www.flickr.com/people/scousevet//">Scousevet<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/scousevet/4535820923//" title=/"Day 109 (April 19th): Marathon Monday/"><img src=/"http://farm5.static.flickr.com/4009/4535820923_f972814468_m.jpg/" width=/"180/" height=/"240/" alt=/"Day 109 (April 19th): Marathon Monday/" /><//a><//p> <p>A day off today for Patriots/' Day. A bitter sweet holiday - yes,I get the day off work,not too shabby,but I was supposed to be celebrating the first battles of the American Revolutionary War. Ah what the heck,I get a day off,I/'ll pretend to celebrate.<br /> <br /> The Boston Marathon is also held every year on Patriots/' Day. Thankfully they had a lovely,dry day for it today. This year/'s winner was a Kenyan runner,he finished in just under 2 hours 6 minutes. Crazy skills.<br /> <br /> Caught Tiddles amidst some patriotic red,white & blue flare while she was bird watching tonight. I/'m off to continue to show my patriotism with some American beer.<//p>","published": "2010-04-19T23:24:18Z","author": "nobody@flickr.com (Scousevet)","author_id": "41073166@N06","tags": "cat bokeh kitty lensflare calico 365 sunflare" },{ "title": "^-^","link": "http://www.flickr.com/photos/misterspells/4535811289/","media": {"m":"http://farm5.static.flickr.com/4041/4535811289_7d8dc16e5f_m.jpg"},"date_taken": "2010-04-18T16:46:18-08:00","description": " <p><a href=/"http://www.flickr.com/people/misterspells//">misterspells<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/misterspells/4535811289//" title=/"^-^/"><img src=/"http://farm5.static.flickr.com/4041/4535811289_7d8dc16e5f_m.jpg/" width=/"240/" height=/"155/" alt=/"^-^/" /><//a><//p> <p>Peek-a-boo!<//p>","published": "2010-04-19T23:20:05Z","author": "nobody@flickr.com (misterspells)","author_id": "63341406@N00","tags": "window cat cincinnati clifton roslynapartments" },{ "title": "cat by the window","link": "http://www.flickr.com/photos/innusa/4535813645/","media": {"m":"http://farm3.static.flickr.com/2698/4535813645_52138e58eb_m.jpg"},"date_taken": "2010-04-04T11:53:14-08:00","description": " <p><a href=/"http://www.flickr.com/people/innusa//">innusa<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/innusa/4535813645//" title=/"cat by the window/"><img src=/"http://farm3.static.flickr.com/2698/4535813645_52138e58eb_m.jpg/" width=/"240/" height=/"160/" alt=/"cat by the window/" /><//a><//p> <p><a href=/"http://bighugelabs.com/flickr/onblack.PHP?id=4535813645&size=large/" rel=/"nofollow/"><b>Large<//b><//a> | <a href=/"http://www.flickr.com/photos/innusa/sets/1470492/"><b>Animal World<//b><//a><//p>","published": "2010-04-19T23:21:05Z","author": "nobody@flickr.com (innusa)","author_id": "45478322@N00","tags": "nature animal cat canon feline 5d canonef2470mmf28lusm kucing 2470mm" },{ "title": "IMG_3039","link": "http://www.flickr.com/photos/rprebello/4535794445/","media": {"m":"http://farm3.static.flickr.com/2502/4535794445_a97bd6bd0a_m.jpg"},"date_taken": "2010-04-18T20:54:29-08:00","description": " <p><a href=/"http://www.flickr.com/people/rprebello//">Rodrigo Rebello<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/rprebello/4535794445//" title=/"IMG_3039/"><img src=/"http://farm3.static.flickr.com/2502/4535794445_a97bd6bd0a_m.jpg/" width=/"162/" height=/"240/" alt=/"IMG_3039/" /><//a><//p> ","published": "2010-04-19T23:12:33Z","author": "nobody@flickr.com (Rodrigo Rebello)","author_id": "11328674@N03","tags": "cat kitty" },{ "title": "","link": "http://www.flickr.com/photos/jillliaaan/4536404200/","media": {"m":"http://farm5.static.flickr.com/4050/4536404200_a94b570fb8_m.jpg"},"date_taken": "2010-03-27T16:17:35-08:00","description": " <p><a href=/"http://www.flickr.com/people/jillliaaan//">jillian nicole.<//a> posted a photo:<//p> <p><a href=/"http://www.flickr.com/photos/jillliaaan/4536404200//" title=/"/"><img src=/"http://farm5.static.flickr.com/4050/4536404200_a94b570fb8_m.jpg/" width=/"240/" height=/"180/" alt=/"/" /><//a><//p> ","published": "2010-04-19T23:01:55Z","author": "nobody@flickr.com (jillian nicole.)","author_id": "32657331@N05","tags": "door cats cat focus kitten dof hand jill kittens jillian jillliaaan" } ] })原文链接:https://www.f2er.com/json/290821.html