我可以在与从该端口加载的脚本文件不同的端口上使用 XMLHttpRequest 吗?

2022-01-15 00:00:00 xmlhttprequest ajax cross-domain

我有使用 XMLHttpRequest(实际上是 jQuery)的网站.我还在同一台服务器上运行了另一个站点,该站点提供一个脚本文件,该脚本文件将 XHR 请求返回到该站点,即.

I have website that use XMLHttpRequest (jQuery, actually). I also have another site running on the same server, which serves a script file that makes XHR requests back to THAT site, ie.

http://mysite:50000/index.html包括

http://mysite:50000/index.html includes

<script src="http://mysite:9000/otherscript.js"></script>

并且http://mysite:9000/otherscript.js包括

and http://mysite:9000/otherscript.js includes

$.ajax({
    url: 'http://mysite:9000/ajax/stuff'
});

问题是 - 这不起作用.来自加载脚本的 AJAX 请求只是失败,没有错误消息.据我所知,这是旧的同源政策.鉴于我控制了这两个站点,我能做些什么来完成这项工作吗?document.domain"技巧似乎对 XMLHttpRequest 没有任何作用.

The problem is - this doesn't work. The AJAX requests from the loaded script simply fail with no error message. From what I've been able to find this is the old same origin policy. Given that I control both sites, is there anything I can do to make this work? The "document.domain" trick doesn't seem to do a thing for XMLHttpRequest.

推荐答案

不,XHR 无法做到这一点.同域策略非常严格——相同的主机、相同的端口、相同的协议.对不起!您必须借助其他技巧(iframe、标题操作等)才能使其正常工作.

Nope- can't do this with XHR. Same-domain policy is very restrictive there- same host, same port, same protocol. Sorry! You'll have to resort to other tricks (iframes, title manipulation, etc) to get it to work.

相关文章