我对此非常陌生,实际上这是我第一次尝试Dojo。我正在尝试从一个网站获取数据,如下所示:
<script
text="text/javascript"
src="http://o.aolcdn.com/dojo/1.3/dojo/dojo.xd.js"
djConfig="parseOnLoad:true,isDebug:true"
></script>
<script type="text/javascript">
//How are we supposed to know what else to include in the dojo thing? like query?
dojo.addOnLoad(function(){
console.log("hi");
dojo.xhrPost({
url: "http://www.scrapbookingsuppliesrus.com/catalog/layout", //who knows how to set relative urls?
handleAs: "json", //get json data from server
load: function(response, ioArgs){
console.log("got");
console.log(response); //helps with the debugging
return response; //that way goods should be a very large array of the data we want
},
error: function(response, ioArgs){
console.log("nope didn't make it", response+' '+ioArgs); //helps with the debugging
return response; //who knows what this does
} //last item, thus no extra comma
});
});
</script>但是什么也没发生。说到这里,response和ioArgs变量到底是什么。它们应该神奇地是对请求的响应,我猜这已经被特别定义了。但是,谁知道呢。此外,我认为在每次尝试之后,它都会在加载或错误中触发一些东西,但遗憾的是。曾经有一个错误,我要去一个被禁止的uri,但是firebug会引用一个非常大的dojo脚本,在那里它不可能知道为什么它被破坏了。您其余的开发环境是什么?
发布于 2009-11-06 03:22:49
好吧,有几个问题。
让我们先从最简单的开始。
当您进行开发时,您希望使用Dojo的“未压缩”版本,该版本可以通过将.uncompressed.js附加到所使用的Dojo库的路径中找到:
http://o.aolcdn.com/dojo/1.3/dojo/dojo.xd.js.uncompressed.js如果是在core-Dojo中,这将使我们更容易看到哪些地方出了问题。
接下来是djConfig参数。我非常确定Dojo可以处理字符串,但传统上它是用对象定义的,所以,一旦包含了Dojo库:
<script src="path to dojo"></script>启动一个新的脚本块,并在其中定义djConfig对象:
<script>
djConfig = {
parseOnLoad: true,
isDebug: true
};
</script>其次是最简单的,我使用Dojo进行开发,它具有内置的IntelliJ代码意义,使生活变得更容易。否则,标准包,Firefox + Firebug。
复杂的东西:
看起来您是在使用XHR方法请求数据,希望您知道这意味着您的脚本和要访问的数据必须驻留在同一个域中,如果它们不在同一个域中,则会出现安全错误。如何解决这个问题呢?您可以使用称为跨域脚本的技术,dojo还通过dojo.io.script.get功能支持这种技术。
更复杂的东西:
Dojo使用一种称为“延迟”对象的东西。这意味着请求实际上不会在对象创建后立即发出,而是在您请求它发出时发出,这就是“延迟”的概念,您将代码块的执行推迟到稍后的时间。在您的案例中,这个问题的解决方法如下:
var deferred = dojo.xhrPost({
url: "http://www.scrapbookingsuppliesrus.com/catalog/layout", //who knows how to set relative urls?
handleAs: "json" //get json data from server
});
if(deferred) {
deferred.addCallback(function(response){
console.log("got");
console.log(response); //helps with the debugging
return response; //that way goods should be a very large array of the data we want
});
deferred.addErrback(function(response){
console.log("nope didn't make it", response+' '+ioArgs); //helps with the debugging
return response; //who knows what this does
});
}现在应该可以解决这个问题了。
作为个人注释,我不建议使用XHR,而是使用dojo.io.script.get方法,从长远来看,它更容易移植。
发布于 2009-11-03 08:47:42
在你的代码中有几个非常基本的错误。
<script ... djConfig="parseOnLoad:true,isDebug:true"/></script>
/> (禁止)的缩写和,同时还包括结束</script>。console.log("hi")
您尝试从加载数据,您的脚本是否也在该域上运行?否则,Crossdomain-Ajax (google it)上的安全限制将阻止您加载数据。
发布于 2009-11-03 13:01:09
这个问题也在dojo-interest列表中被提出,并且有一些对问题on that list的回答。
https://stackoverflow.com/questions/1664273
复制相似问题