2010-04-06 81 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是動態服務的服務器端。