問題描述
這是目標:
移交引用遠程服務(wù)器上的 JavaScript 文件的腳本標記.該 JavaScript 文件應(yīng)返回 HTML,然后將顯示在調(diào)用 HTML 頁面上.
Hand off a script tag that references a JavaScript file on a remote server. That JavaScript file should return HTML that will then be displayed on the calling HTML page.
我嘗試通過兩種方式解決這個問題:
I've attempted to approach this in two ways:
首先,我嘗試在 JavaScript 中使用 XMLHttpRequest 來調(diào)用這個遠程服務(wù)器.在 IE 中,它會按預(yù)期工作,但 FF、Safari 和 Chrome 會返回一個空響應(yīng).我從研究中得到的總體反應(yīng)是,請求被阻止,因為它嘗試訪問的服務(wù)器與其運行的位置不同(都是本地主機,但端口不同).
First, I attempted to use XMLHttpRequest in JavaScript to call this remote server. In IE, it would work as expected but FF, Safari, and Chrome would return an empty response. The overall response I got from my research was that the request was being blocked since the server it's trying to access is different from where it's running (both localhost, but different ports).
其次,我研究了 Google Gadgets 之類的東西是如何工作的,因為它們有效地為您提供了一個引用外部 JavaScript 的簡單腳本標記.據(jù)我所知,似乎有某種 iframe 操作僅通過使用的基本 url 進行(下面的示例).這似乎是要走的路,即使使用 iframe 不是我最初的想法.我猜 Google 代碼會將 iframe 作為 HTML 返回到嵌入了此腳本的 HTML 文件.
Second, I looked at how things like Google Gadgets work as they effectively give you a simple script tag that's referencing external JavaScript. From what I can gather, it appears that there's some sort of iframe action going on simply by the base url being used (example below). This appears to be the way to go, even though using an iframe wasn't my initial thought. I'm guessing the Google code is returning an iframe as HTML to the HTML file that has this script embedded.
關(guān)于我應(yīng)該如何進行的任何建議?
Any suggestion on how I should proceed?
<script src="http://www.gmodules.com/ig/ifr?url=http://ralph.feedback.googlepages.com/googlecalendarviewer.xml&synd=open&w=320&h=200&title=&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js"></script>
推薦答案
JSONP是處理同源策略的一種非常常見的方式.由于大多數(shù) javascript 框架(例如 jquery)已經(jīng)支持它,因此您無需了解技術(shù)細節(jié)即可使用它.
您也可以通過從 javascript 構(gòu)造 script
標記(如您所提到的)來自己完成這個技巧.谷歌分析代碼片段就是這種方法的一個例子:
JSONP is a very common way to deal with same origin policy. Since most javascript frameworks (e.g., jquery) already support it, you don't have to get into technical details to use it.
You can also do the trick yourself by constructing script
tag from javascript (as you mentioned). Google Analytics code snippet is an example of such approach:
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'url here';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
至于 iframe 的想法(如果我理解你的話),它是行不通的.您可以在頁面上使用 iframe
元素來顯示來自另一臺服務(wù)器的內(nèi)容,但瀏覽器不允許您使用 javascript 從主頁訪問它.
As for iframe idea (if I understand you currectly), it's not gonna work. You can use iframe
element on your page to display content from another server, but browser won't let you access it from main page using javascript.
編輯
這個原始提案詳細介紹了 JSONP 的用法:
http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
這篇關(guān)于跨域進行 JavaScript 調(diào)用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!