2010-04-06 75 views
0

我在博客上添加了最近的视频小工具。在该插件我应该添加此行这个javascript做什么?

<script src="/feeds/posts/default?orderby=published&alt=json-in-script&callback=showrecentpostswiththumbs"> 

另外,我加入其具有方法showrecentpostswiththumbs [在回调用于]另一个脚本。请让我知道上面的语法做什么?

编辑 主的评论:)后

其实,我的博客托管在blogspot.com。所以从这个角度来看,如果你将追加/feeds/posts/default?orderby =发布& alt = json-in-script到任何博客网址,它都会生成一些代码。我只是想知道它做了什么?以及回调中使用的方法参数[不管回调方法的定义是什么:)]。

为如:http://googleblog.blogspot.com /供稿/职位/默认排序依据=出版& ALT = JSON-在脚本

+0

您发布的内容是HTML以在网页中包含一些JavaScript。至少在不知道域名的情况下,我们无法确定它引用的是哪个代码。你能发布实际的JavaScript吗? – Pops 2010-04-06 18:17:28

+0

@主::)。在任何博客后面添加'/ feeds/posts/default?orderby = published&alt = json-in-script'[我认为只有blogspot上的博客才能工作但不确定。]。例如: - http://googleblog.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script – 2010-04-06 18:22:22

回答

3

根据您发布的内容无法判断,但URL 中的参数命名建议JSONP

JSONP的基础知识是通过在一个函数调用中包装另外的裸JSON对象来允许跨域AJAX调用,以便结果可以作为脚本执行。

JSON代码:

function getJSON(url) { 
    var xhr = new XHR(url); // pseudocode 
    xhr.onsuccess = callback; 
    xhr.send(); 
} 

function callback(data) {} 

JSON响应:

{ "items" : [1, 5, 7] } 

等效JSONP代码:

function getJSONP(url) { 
    var script = document.createElement("script"); 
    script.src = url + "&callback=callback"); 
    script.type = "text/javascript"; 
    document.body.appendChild(script); 
} 

function callback(data) {} 

JSONP响应:

callback({ "items" : [1, 5, 7] }) 

编辑

它是JSONP。比较以下三个请求的结果:

第一返回料为原料,JSON,第二个返回它作为JSONP一个默认的回调名称,第三个使用提供的回调函数名称将其作为JSONP返回。

0

它不是一个脚本,但到外部脚本文件的链接,因为你正在使用一个相对路径,我们看不到实际的脚本。

+1

请解释downvote!这是一个合法的答案。 – systempuntoout 2010-04-06 18:39:56

1

据说,这脚本返回包含像这样的脚本:

showrecentpostswiththumbs({ /* some JSON object */ }); 

具有功能showrecentpostswiththumbs其他脚本。该函数可能用于接收JSON对象并对其进行一些处理。

+0

是的,_showrecentpostswiththumbs_正在使用json属性。我应该发布showrecentpostswiththumbs的代码。 PS:BIG代码;) – 2010-04-06 18:29:38

0
<script type="text/javascript" src="myscripts.js"></script> 

src属性指定外部脚本文件的URL。
在你的情况下,js是动态服务的服务器端。