我可以在从该端口加载的脚本文件的不同端口上使用XMLHttpRequest吗? [英] Can I use XMLHttpRequest on a different port from a script file loaded from that port?

查看:244
本文介绍了我可以在从该端口加载的脚本文件的不同端口上使用XMLHttpRequest吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网站使用XMLHttpRequest(jQuery,实际上)。我也有另一个网站运行在同一个服务器上,它提供一个脚本文件,使XHR请求回到THAT网站,即

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.

这篇关于我可以在从该端口加载的脚本文件的不同端口上使用XMLHttpRequest吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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