問題描述
我有使用 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.
這篇關于我可以在與從該端口加載的腳本文件不同的端口上使用 XMLHttpRequest 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!