有没有办法绕过 Javascript / jQuery 的本地访问同源策略?

发布于 2024-09-14 15:47:18 字数 112 浏览 22 评论 0 原文

尝试使用 ajaxgetJSON 和类似函数从本地(非服务器)开发计算机获取外部 URL。有没有办法绕过同源策略,以便我可以在本地测试,而不必上传到服务器?

Trying to use ajax, getJSON, and functions like that to fetch an external URL from a local (non-server) development computer. Is there a way to bypass the same origin policy, so that I can test locally, instead of having to upload to a server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

失眠症患者 2024-09-21 15:47:18

这是简单的答案: chrome --disable-web-security

从源代码(chrome_switches.h):

// Don't enforce the same-origin policy.  (Used by people testing their sites.)
const char kDisableWebSecurity[]            = "disable-web-security";

我想使用 jquery.js 将 AJAX 调用发送到在端口 8080 上运行的 Google Apps python 服务器。只是为了测试,我想要在同一台机器上运行浏览器和服务器。

我不明白所有的安全细微差别,但对于临时开发来说,这似乎是一个合理的解决方法。只要我只使用 chrome 来测试这个标志,就不会有问题。

以下是 Mac OS X 的完整命令:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security

Here's the simple answer: chrome --disable-web-security

From the source code (chrome_switches.h):

// Don't enforce the same-origin policy.  (Used by people testing their sites.)
const char kDisableWebSecurity[]            = "disable-web-security";

I wanted to use jquery.js to send AJAX calls to a Google Apps python server running on port 8080. Just for testing, I wanted to run the browser and the server on the same machine.

I don't understand all the security nuances, but for temporary development it seems like a reasonable workaround. So long as I only use chrome for testing with this flag, it shouldn't be a problem.

Here's the whole command for Mac OS X:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security

花辞树 2024-09-21 15:47:18

我们在开发网络应用程序时也有同样的需求。我们是这样做的:

浏览器和服务器仅通过 JSON 进行通信。
所有 HTML 均使用 PURE(我们的 JS 模板引擎)在浏览器中呈现。
浏览器代码是这样在本地开发的:

我们在应用程序的 url 中添加一个 host 参数:

http://localhost/app.html?host=test.beebole-apps.com

在生产中,JSON 通过 POST 发送到服务器。
但这里负责 ajax 调用的函数将对 host 参数做出反应,并生成一个 JSONP 注入(GET)。

<script src="http://test.beebole-apps.com/?callback=f2309892&json={...}" />
  • f2309892 是一个临时函数,具有随机名称,它指向处理响应的方法
  • json 是我们发送到服务器的 JSON

这意味着您将需要一些后端的合作为您提供封装在回调函数中的 json,例如:

f2309892( /*the json here*/ );

除了大小限制(您不能使用 GET 向服务器发送大 JSON)之外,它的工作方式轻而易举。
另一个优点是您可以从同一个本地主机调用所有不同的系统(开发和测试)。

We had the same need when developing our web app. Here's how we did it:

The browser and the server communicate only through JSON.
All the HTML is rendered in the browser using PURE (our JS template engine).
The browser code is developed locally like this:

We add a host parameter in the url of the app:

http://localhost/app.html?host=test.beebole-apps.com

In production, the JSON are sent to the server with a POST.
But here the function in charge of the ajax call will react to the host parameter and make a JSONP injection(GET) instead.

<script src="http://test.beebole-apps.com/?callback=f2309892&json={...}" />
  • f2309892 is a temporary function, with a random name, that points to the method that will handle the response
  • json is the JSON we send to the server

It means you will need some cooperation from the backend to serve you the json wrapped in a callback function like:

f2309892( /*the json here*/ );

Except a size limitation(you can't send a big JSON to the server with a GET) it works like a breeze.
An other advantage is you can call all the different systems(development and test) from the same localhost.

骑趴 2024-09-21 15:47:18

有不同的方法可以解决这个问题,具体取决于您用于开发的浏览器。例如:

  • 在 Firefox (Gecko) 中,将 security.fileuri.strict_origin_policy 设置为 false
  • 在 Chrome 中,使用选项 --allow-file-access 启动浏览器-from-files

参考:FirefoxChrome

There are different ways to get around this, depending on which browser you're using for development. For example:

  • In Firefox (Gecko), set security.fileuri.strict_origin_policy to false
  • In Chrome, start the browser with the option --allow-file-access-from-files

References: Firefox, Chrome

<逆流佳人身旁 2024-09-21 15:47:18

无需接触服务器 -

在 Firefox 中绕过同源安全策略的最快、最简单的方法是安装 Force CORS 插件。通过将正确的标头插入到每个响应中,这适用于任何服务。

https://addons.mozilla.org/en-US/firefox/addon/力量/

Without touching the server -

The quickest and easiest way to bypass the same origin security policy in Firefox is the install the Force CORS add-on. This works with any service by inserting the proper headers into every response.

https://addons.mozilla.org/en-US/firefox/addon/forcecors/

甜心小果奶 2024-09-21 15:47:18

由于这是一个开发问题而不是最终用户/功能问题,因此不要专注于让 AJAX 跨域,而是将您的开发环境设置为代理以从生产服务器获取最新数据。这实际上很容易做到。

您需要在开发环境中设置一台 Web 服务器(如果还没有),然后将服务器配置为通过获取并回显生产数据来响应 404 请求。您可以设置您的服务器,以便仅拾取 AJAX 数据文件(否则,如果生产资产开始显示在您的开发页面上,调试其他文件将会很混乱)。因此,如果 http://dev.myserver.com/data/json/mydata.json 丢失,您的 404 脚本将得到 http://prod.myserver.com/data/json /mydata.json 并将其回显给客户端。这种设置的好处是您可以非常轻松地使用模拟数据:如果您的开发环境中存在该文件,您的 AJAX 脚本就会获取该文件;但如果您随后删除或重命名该文件,您将获得生产数据。这个功能非常有用,我强烈推荐它。

如果您使用 XML,我建议您复制 404 中的 HTTP 标头。如果您的 404 进程响应 Content-Typetext/html,那么您不会获得任何要解析的 responseXML

Since this is a development issue and not a end-user/functionality issue, rather than focusing on getting AJAX to cross domains get your development environment set up as a proxy to fetch the most recent data from the production servers. This is actually really easy to do.

You'd need to set up a web server in your dev environment (if it doesn't have one already), and then configure the server to respond to 404 requests by fetching and then echoing production data. You can set up your server so that only the AJAX data files are picked up (otherwise, it will be confusing to debug other files if production assets start showing up on your development pages). So if http://dev.myserver.com/data/json/mydata.json is missing, your 404 script will get http://prod.myserver.com/data/json/mydata.json and echo it to the client. The nice thing about this set-up is that you can use mock data very easily: if the file is there in your dev environment, your AJAX script will get that; but if you then erase or rename that file, you'll get the production data instead. This feature has been so useful I can't recommend it enough.

If you're working with XML, I'd recommend duplicating the HTTP headers in the 404. If your 404 process responds with a Content-Type of text/html, you won't get any responseXML to parse.

楠木可依 2024-09-21 15:47:18

我也遇到了这个问题,使用 Chrome 并且 --allow-file-access-from-files 选项并没有真正帮助。回到我的服务器需要返回的脚本,我将这些标头添加到响应中并且工作正常:

'Access-Control-Allow-Origin: http://localhost/'

另一个标头用于允许某种密钥交换

'Access-Control-Allow-Headers: X-KEY'

I had that problem, too, using Chrome and the --allow-file-access-from-files option didn't really help. Back to the script my server needed to return, I added these headers to the response and it worked fine :

'Access-Control-Allow-Origin: http://localhost/'

and another one for allowing a sort of key exchange

'Access-Control-Allow-Headers: X-KEY'
长途伴 2024-09-21 15:47:18

不允许在 CORS 中使用 localhost http://code.google.com /p/chromium/issues/detail?id=67743 使用 lvh.me 代替

localhost is not allowed to use in CORS http://code.google.com/p/chromium/issues/detail?id=67743 use lvh.me instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文