Ajax POST 到另一台服务器 - 克服跨域限制

发布于 2024-11-05 19:03:58 字数 713 浏览 1 评论 0原文

使用代理是实现此目的的唯一正确方法吗?我正在研究 clicktail 克隆(网络用户交互和鼠标跟踪分析),到目前为止我拥有的是一个脚本,该脚本将跟踪所有用户交互并将它们发布到我的服务器并保存到数据库,但是问题如果将此脚本添加到位于另一个域的客户端,则 Ajax 发布将由于跨域脚本限制而失败。

通过阅读本文,我发现有一些黑客行为,但我渴望避免它们。看来代理是解决这个问题的唯一方法。但这反过来又给我带来了一个更大的问题,因为任何希望使用我的 clicktail 克隆的客户除了在其上添加 JavaScript 之外还必须设置代理。对于拥有 PHP 网站的网站管理员来说,这将是直接的,因为我可以提供脚本,他们只需将其放在他们的服务器上,但对于 .net 网站,他们必须将其包含在他们的解决方案中并重新编译,也许我可以提供一个 dll,但即使如此也必须引用。

我一直在阅读有关 HTML 5 window.postMessage -> 的内容和这个jquery插件 http://benalman.com/projects/jquery-postmessage-plugin/ 这看起来不错,我认为我能够使用它来播放捕获的用户交互,使用 Iframe,但它不能(至少我不知道如何)解决跨域 AJAX 发布的问题。 如果您处于我的情况,您能发布您可能的解决方案吗?

Is the only proper way to achieve this by using a proxy? I'm working on a clicktail clone (web user interaction & mouse tracking analytics) and what I have so far is a script that will track all the user interactions and will post them to my server and save to the database, but the issue is if this script is added to a client who will be on another domain, the Ajax post will fail due to the cross domain scripting restrictions.

from reading about this, there are some hacks, but im keen to avoid them. It seems that a proxy is the only way round this. But that in turn poses a larger issue for me as any client who wishes to use my clicktail clone will also have to setup a proxy in addition to added javascript on their. This will be straight forward for a webmaster with a PHP site, as I can provide the script and they just bang it on their server, but for .net sites, they would have to include that in their solution and recompile, maybe i could provide a dll, but even that would have to be referenced.

I have been reading about this HTML 5 window.postMessage -> and this jquery plugin http://benalman.com/projects/jquery-postmessage-plugin/ this looks good, and I think ill be able to use it to playback the captured user interactions, using Iframes but It doesn't, (at least I cannot see how) resolve the issue with cross domain AJAX posting.
if you were in my situation, could you post your possible solution ?

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

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

发布评论

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

评论(1

原来是傀儡 2024-11-12 19:03:58

在你的情况下,跨域 POST 似乎确实是可能的。
这里 如何发送跨域 POST通过 JavaScript 请求? 讨论了使用 Access-Control-Allow-Origin 配置接收服务器的可用性。
之后你可以通过 jQuery 进行 POST:

$.ajax({
    type: 'POST',
    url: 'https://to.com/postHere.php',
    crossDomain: true,
    data: '{"some":"json"}',
    dataType: 'json',
    success: function(responseData, textStatus, jqXHR) {
        var value = responseData.someKey;
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

祝你好运!

It seems that cross-domain POSTs are really possible in your situation.
Here How do I send a cross-domain POST request via JavaScript? discussed the availability to configure the receiving server with Access-Control-Allow-Origin.
After that you can do POST via jQuery:

$.ajax({
    type: 'POST',
    url: 'https://to.com/postHere.php',
    crossDomain: true,
    data: '{"some":"json"}',
    dataType: 'json',
    success: function(responseData, textStatus, jqXHR) {
        var value = responseData.someKey;
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

Good luck!

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