AJAX、子域和 200 OK 响应
一个非假设但抽象的情况:
我有一个域 www.foo.com,我从该域向 beta.foo.com 发送 AJAX POST。检查 XHR 对象,我看到响应标头为 200 OK,但没有响应文本 - 我什至得到一个 12B 长的响应,这是我期望的确切响应(一个 12 个字符的字符串) - 但响应文本是空白的。
如果这是一个跨域问题,为什么我会得到 200 OK,甚至更好 - 为什么我看到 PHP 函数在 beta.foo.com 端触发 - 但没有得到任何响应?
A non-hypothetical but abstracted situation:
I have a domain www.foo.com, from which I'm making an AJAX POST to beta.foo.com. Examining the XHR object, I see a response header of 200 OK, but no response text - I even get a response 12B long, which is the exact response (a 12-character string) that I'm expecting - but the response text is blank.
If this is a cross-domain issue, why am I getting 200 OK, and better yet - why am I seeing the PHP functions fire on the beta.foo.com side - yet getting no response?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能那么容易地进行跨子域 ajax 调用。有一种名为同源策略的东西可以阻止您这样做。如果您想解决此问题,您需要使用 JSONP 或 < a href="http://ajaxpatterns.org/IFrame_Call" rel="nofollow noreferrer">Iframe。
安装 firebug,您将看到一个 http 200 代码和一个错误:该错误是 SOP 行为。
You can't do cross subdomains ajax calls that easy. There is something called Same origin policy that prevents you from doing that. If you want sort this issue you need to use JSONP or Iframes.
Install firebug and you will see an http 200 code and an error: that error is SOP acting.
您提到您正在检查responseText 属性。您的回复可能是 XML 格式吗?
如果您发送 XML 请求,或者响应类型为
'text/xml'
,您将获得responseXML
的值。我相信如果responseText
属性采用 XML 格式,则可以为空。来自谷歌的随机示例:
http://javascript.about.com/library/blajax08.htm
You mentioned that you're checking the responseText property. Is it possible your response is in XML format?
If you send an XML request, or the response type is
'text/xml'
, you will get a value forresponseXML
. I believe that theresponseText
property can be blank if it's in XML format.Random example from google:
http://javascript.about.com/library/blajax08.htm