当主机名不同时 jquery 加载中断
该代码段来自名为“sites-catchup.html”的页面。 有一个名为“max-change-num-ajax.html”的简单服务器端页面,它返回一个数字(没有任何 html)。
如果通过调用 http://tiddy:8080/rsm/sites-catchup.html 请求该页面 然后一切正常。
但是,如果我调用 http://localhost:8080/rsm/sites-catchup.html 页面正常呈现,但 ajax 位总是失败。 Tiddy 和 localhost 有不同的 IP 地址,但指向同一台机器。如果我切换它们,以便从本地主机加载“sites-catchup.html”,但我从 tiddy 请求页面,那么它也不起作用。
如果负载更改为本地主机并且我请求本地主机,那么它就可以工作。
在 Firebug 中,当主机名相同时,加载会执行 GET 请求,返回正确的值,但如果主机名不同,则加载会执行 OPTIONS 命令,该命令始终不返回任何内容。
这样做的最终目的是在一个表中,其中每一行都具有从不同机器拉入的状态,因此它永远不会与您请求页面的主机相匹配。
似乎找不到发生这种情况的任何原因或如何解决它。有人可以帮忙吗?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<link type="text/css" href="js/jquery/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jquery/jquery-ui-1.7.2.custom.js"></script>
<!--[if gte IE 5.5]>
<SCRIPT language="JavaScript" src="js/jquery.ienav.js" type="text/javascript"></SCRIPT>
<![endif]-->
</HEAD>
<div id="rsmmax1">Fetching1...</div>
<script type="text/JavaScript">
$(document).ready(function() {
$("#rsmmax1").load("http://tiddy:8080/rsm/max-change-num-ajax.html");
});
</script>
</BODY>
</HTML>
The code segment is from a page called "sites-catchup.html".
There is a simple server side page called "max-change-num-ajax.html" which returns a single number (without any html).
If the page is requested by calling http://tiddy:8080/rsm/sites-catchup.html then it all works.
However if I call http://localhost:8080/rsm/sites-catchup.html the page renders as normal but the ajax bit always fails. Tiddy and localhost have different ip addresses but point to the same machine. If I switch them around so that "sites-catchup.html" loads from localhost, but I request the page from tiddy, then it doesn't work either.
If the load is changed to localhost and I request localhost then it works.
Looking in Firebug, when the host names are the same the load performs a GET request which returns the correct value, but if the host names are different then the load performs an OPTIONS command which always returns nothing.
The ultimate purpose of this will be in a table where each row will have a status pulled in from a different machine, so it will never match the host from which you are requesting the page.
Can't seem to find any reason why this is happening or how to fix it. Can anyone help?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<link type="text/css" href="js/jquery/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jquery/jquery-ui-1.7.2.custom.js"></script>
<!--[if gte IE 5.5]>
<SCRIPT language="JavaScript" src="js/jquery.ienav.js" type="text/javascript"></SCRIPT>
<![endif]-->
</HEAD>
<div id="rsmmax1">Fetching1...</div>
<script type="text/JavaScript">
$(document).ready(function() {
$("#rsmmax1").load("http://tiddy:8080/rsm/max-change-num-ajax.html");
});
</script>
</BODY>
</HTML>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设计使然。出于安全原因,您无法执行跨域 AJAX 请求。不过,您可以使用 JSONP 。另一种选择是使用代理< /a>.
This is by design. For security reasons you cannot perform cross domain AJAX requests. You could use JSONP though. Another option is to use a proxy.