这是跨域问题吗?
我有一个 jQuery .ajax() 调用,在 IE 7 和 8 中工作正常。它在 FF 或 Chrome 中不起作用。我认为这是一个跨域问题(因为它试图跨域调用),但我的同事说,如果这是一个跨域问题,那么它在任何浏览器中都不起作用。我说得对吗?跨域问题是否会出现在某些浏览器中而在其他浏览器中则不会?对这个东西有点新...谢谢!
更新:这是我的代码,使用测试 xml(与我尝试显示的属性不匹配,但我只是想了解我的 ajax 调用出了什么问题):
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://www.w3schools.com/xml/note.xml", //test xml
dataType: "xml",
success: xmlParser,
asynch: true
});
});
function xmlParser(xml) {
$(xml).find("Offer:lt(3)").each(function () {
$("#offers").append('<img src="' + $(this).find("logophotoname").text() + '"/><h1>' + $(this).find("listTitle").text() + '</h1><p>' + $(this).find("keywords").text() + '</p>');
});
}
I have a jQuery .ajax() call that works fine in IE 7 and 8. It does not work in FF or Chrome. I thought it was a cross-domain issue (because it is trying to call across domains) but my co-worker says if it were a cross-domain problem it wouldn't work in any browsers. Am I correct? Can a cross-domain issue appear in some browsers and not in others? Kinda new to this stuff...thanks!
UPDATE: Here is my code, using a test xml (won't match the attributes I try to display, but I'm just trying to get a feel for what's wrong with my ajax call):
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://www.w3schools.com/xml/note.xml", //test xml
dataType: "xml",
success: xmlParser,
asynch: true
});
});
function xmlParser(xml) {
$(xml).find("Offer:lt(3)").each(function () {
$("#offers").append('<img src="' + $(this).find("logophotoname").text() + '"/><h1>' + $(this).find("listTitle").text() + '</h1><p>' + $(this).find("keywords").text() + '</p>');
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的同事是对的。它不应该在任何浏览器中工作。然而,在 IE 8 中,微软引入了另一个类似于 XmlHttpRequest 的对象 -> XDomainRequest 但我不认为你正在使用它,否则 IE 7 会失败。 (更多信息请点击此处)
jQuery的功能可用于使用JSONP进行跨域调用。然后,它将在 DOM 树中注入一个脚本对象,而不是使用具有跨域限制的 XmlHttpRequest 对象。
(整篇文章仅与您的解释相关,如果您发布一些代码将会有所帮助。也许还有其他原因它在 IE 7/8 中工作)
Your co-worker is correct. It should not work in any browser. However in IE 8 Microsoft introduced another object similar to the XmlHttpRequest -> XDomainRequest but I don't think you are using that, else IE 7 would fail. (more info here)
The ajax function of jQuery can be used to make a cross-domain call using JSONP. It will then inject an script object in the DOM tree instead of using a XmlHttpRequest object which has cross-domain restrictions.
(This whole post is related to your explanation only, it will help if you post some code. Maybe there's an other reason why it works in IE 7/8)
理论上你不能进行跨域调用,但如果你真的想这样做,有两种选择:
1.有一个基于服务器的代理脚本
2.JSONP
In theory you cannot make cross-domain calls, but if you really want to, there are 2 options:
1. have a server based proxy script
2. JSONP