dojo.xhrGet或dojo.xhrPost
我对此很新,事实上这是我第一次尝试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> 但没有任何反应.虽然我在这,但究竟是什么响应和ioArgs变量.它们应该神奇地是对我认为已经特别定义的请求的响应.但是,谁知道呢.此外,我想在每次尝试之后它会在加载或错误中触发某些东西,但是唉. 解决方法
好吧,有几个问题.
让我们先从非常简单的开始. 在开发时,您希望使用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> 接下来最简单,我使用IntelliJ JIDEA进行开发,它具有内置的Dojo代码感,使生活更轻松.否则,标准包,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方法,从长远来看,这种方法更加便携. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |