跨域调用JavaScript [英] Making JavaScript call across domains

查看:190
本文介绍了跨域调用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的目标是:

移交引用远程服务器上的JavaScript文件的脚本标记。该JavaScript文件应该返回HTML,然后将显示在调用的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来调用这个远程服务器。在IE中,它会正常工作,但FF,Safari和Chrome将返回一个空响应。我从我的研究得到的总体反应是,请求被阻止,因为它试图访问的服务器不同于它运行的地方(localhost,但不同的端口)。

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的简单脚本标记。从我可以收集,似乎有一些类型的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.

有关如何处理的任何建议?

Any suggestion on how I should proceed?

<script src="http://www.gmodules.com/ig/ifr?url=http://ralph.feedback.googlepages.com/googlecalendarviewer.xml&amp;synd=open&amp;w=320&amp;h=200&amp;title=&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"></script>


推荐答案

JSONP 是一种非常常见的处理同源政策。由于大多数JavaScript框架(例如jquery)已经支持它,你不必得到技术细节来使用它。

你也可以通过构造脚本来自javascript的标记(如上所述)。 Google Analytics(分析)代码段是此类方法的一个示例:

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 元素来显示来自其他服务器的内容,但浏览器不会允许您使用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/ < a>

edit
This original proposal details JSONP usage:
http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

这篇关于跨域调用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆