小弟新手,最近学到jsonp和ajax时遇到疑惑。 $.getJSON()中的url是怎么来的???并不是网页的网站啊,还需要各种参数还有callback~~ 这个url是网站提供的开放url来获取json数据的还是怎样的???这个开放的url和各网站的开放平台有关吗????假如我想获取某个新闻网的 一些文章标题做成超链接,也就是获取json数据,我要怎么写url????就结了一天了 ,百度不到答案啊。。问题有点多,请各位大神帮我一次,耐心解答下吧,感激不尽。
解决方案 »
- 为什么再减小的时候出错了
- 怎样将指定页面中的图片通过javascript导入到Excel文档
- 这段代码为什么错?抱Microsoft JScript 运行时错误 错误 '800a1391'
- 请问在javascript中怎么把另一个页面的内容全部输出到本页面中??
- javascript得到鼠标坐标的问题
- chrome为什么不显示这段js代码?
- 判断文本框内容改变
- 得到某一年的第一个星期天的算法,是怎么样的啊?
- 如何用JS的FileSystemObject 改变某一行的值
- 如何刷新父窗口的父窗口?
- JQuery中这个function(e)那个e是什么意思?求指教
- javascript中String.fromCharCode方法传的是unicode吗
callback是你请求会数据后做什么吧?
我是猜的
jQuery.getJSON( url [, data ] [, success(data, textStatus, jqXHR) ] )
url
Type: String
A string containing the URL to which the request is sent.
data
Type: PlainObject
A plain object or string that is sent to the server with the request.
success(data, textStatus, jqXHR)
Type: Function()
A callback function that is executed if the request succeeds.
function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});
其中http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?
返回json数据,然后在回调函数里用$.each遍历json数据的data项数据,最后取出data项数据中item子项的数据……例如返回的json为({
"title": "Recent Uploads tagged cat",
"link": "http://www.flickr.com/photos/tags/cat/",
"description": "",
"modified": "2013-01-19T23:03:02Z",
"generator": "http://www.flickr.com/",
"items": [
{
"title": "Sunset #12",
"link": "http://www.flickr.com/photos/81737232@N03/8395416595/",
"media": {"m":"http://farm9.staticflickr.com/8353/8395416595_570bef797b_m.jpg"},
"date_taken": "2013-01-19T17:58:54-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8395416595/\" title=\"Sunset #12\"><img src=\"http://farm9.staticflickr.com/8353/8395416595_570bef797b_m.jpg\" width=\"240\" height=\"180\" alt=\"Sunset #12\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:03:02Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
},
{
"title": "Sunset #9",
"link": "http://www.flickr.com/photos/81737232@N03/8395407155/",
"media": {"m":"http://farm9.staticflickr.com/8095/8395407155_3f1a30ed67_m.jpg"},
"date_taken": "2013-01-19T18:00:24-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8395407155/\" title=\"Sunset #9\"><img src=\"http://farm9.staticflickr.com/8095/8395407155_3f1a30ed67_m.jpg\" width=\"240\" height=\"180\" alt=\"Sunset #9\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:03:04Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
},
{
"title": "Ocean",
"link": "http://www.flickr.com/photos/81737232@N03/8396596866/",
"media": {"m":"http://farm9.staticflickr.com/8499/8396596866_7cb935cb3f_m.jpg"},
"date_taken": "2013-01-19T17:18:22-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8396596866/\" title=\"Ocean\"><img src=\"http://farm9.staticflickr.com/8499/8396596866_7cb935cb3f_m.jpg\" width=\"180\" height=\"240\" alt=\"Ocean\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:02:39Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
},
{
"title": "Clouds",
"link": "http://www.flickr.com/photos/81737232@N03/8396508296/",
"media": {"m":"http://farm9.staticflickr.com/8333/8396508296_a522aedcc4_m.jpg"},
"date_taken": "2013-01-19T17:58:03-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8396508296/\" title=\"Clouds\"><img src=\"http://farm9.staticflickr.com/8333/8396508296_a522aedcc4_m.jpg\" width=\"180\" height=\"240\" alt=\"Clouds\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:03:00Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
},
{
"title": "Cloud #1",
"link": "http://www.flickr.com/photos/81737232@N03/8395530223/",
"media": {"m":"http://farm9.staticflickr.com/8088/8395530223_ffb0d41d55_m.jpg"},
"date_taken": "2013-01-19T17:10:03-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8395530223/\" title=\"Cloud #1\"><img src=\"http://farm9.staticflickr.com/8088/8395530223_ffb0d41d55_m.jpg\" width=\"180\" height=\"240\" alt=\"Cloud #1\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:02:36Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
},
{
"title": "IMG_2610",
"link": "http://www.flickr.com/photos/81737232@N03/8395426447/",
"media": {"m":"http://farm9.staticflickr.com/8501/8395426447_fac5e5bbfb_m.jpg"},
"date_taken": "2013-01-19T17:52:39-08:00",
"description": " <p><a href=\"http://www.flickr.com/people/81737232@N03/\">►Raquel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/81737232@N03/8395426447/\" title=\"IMG_2610\"><img src=\"http://farm9.staticflickr.com/8501/8395426447_fac5e5bbfb_m.jpg\" width=\"180\" height=\"240\" alt=\"IMG_2610\" /><\/a><\/p> <p><\/p>",
"published": "2013-01-19T23:03:00Z",
"author": "[email protected] (►Raquel)",
"author_id": "81737232@N03",
"tags": "ocean trees light boy sunset sky sun moon lighthouse tree eye love portugal nature girl smile clouds cat canon mouth photography glasses photo fishing focus kitten rocks waves peace hand blu obey cap blond moonlight swag moutains supreme vinatge"
}
]
})
那么那些开放平台的api给的地址后面没有callback=?,是要自己加上&callback=?还是直接就把api的地址放在$.getJSON(url,callback)中,再定义callback函数。取得的json数据要先encodeurl再遍历看看里面有什么内容吗?