我在http://localhost:8081/dojo-1.3.2/dojo/dojo.js处有一个dojo的本地部署 对于我的自定义模块,我在http://localhost:8081/dojo-1.3.2/explosive/space/Modulator.js创建了以下javascript文件:
dojo.provide("explosive.space.Modulator");
dojo.declare("explosive.space.Modulator",null,{
bob:1
});
在我的html页面中,我有以下内容:
<script type="text/javascript" src="http://localhost:8081/dojo-1.3.2/dojo/dojo.js"></script>
<script type="text/javascript">
dojo.require("explosive.space.Modulator");
</script>
当我在firefox中启动我的网站时,我收到错误:
错误:无法加载 'explosive.space.Modulator';持续 试着 ” ../炸药/空间/ Modulator.js'
关于我做错的任何想法?
编辑:经过更多测试后,看起来代码适用于IE8和IE8兼容模式,但不适用于Firefox,Chrome或Opera。
EDIT2:我的html文件位于不同的域名,但我认为这不应该有任何区别。在阅读了Seth的评论后(感谢Seth!),我尝试了两种方式。
我有两个tomcat实例,一个在端口8080上,另一个在端口8081上。我在两个实例中放置了index.htm并启动它们。
http://localhost:8081/test/index.htm适用于FF和IE。 http://localhost:8080/test/index.htm在FF中没有按预期工作,但在IE中可以正常工作。
显示XmlHTTPRequests显示FF正在调用“GET http://localhost:8081/dojo-1.3.2/explosive/space/Modulator.js”,由于某种原因,它会返回空响应。
如果有任何帮助,这是我的index.htm代码的全部内容。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>Create Map</title>
<script type="text/javascript" src="http://localhost:8081/dojo-1.3.2/dojo/dojo.js"></script>
<script type="text/javascript">
dojo.require("explosive.space.Modulator");
function init(){
var eludiumFuel36 = new explosive.space.Modulator();
alert(eludiumFuel36.bob);
}
dojo.addOnLoad(init);
</script>
</head>
<body class="tundra">
<h1>Hello World!</h1>
</body>
</html>
答案 0 :(得分:2)
你看到的东西看起来不错。当你打开页面时,你会去http://localhost:8081/testpage.html(或某些人)?或者你要去另一个域?
您是否安装了firebug?如果没有,请安装它并打开“Show XMLHttpRequests”。这将显示它试图在控制台中访问的URL(以及任何其他可能相关的错误)。
否则,除了你发布的与dojo的加载路径混淆之外,页面上可能还有其他内容。
修改强>
当dojo执行dojo.require时,它会对该文件执行XHR调用。包括主机和端口。因此,当html页面在localhost:8081上时,它违反了跨域安全性,但javascript在localhost:8080上。您可以使用文件的cross-domain build version来解决这个问题(或确保所有请求都在同一个端口和主机上)。说实话,我不太清楚为什么IE可以使用不同的端口。