【问题标题】:dojo xhrPost issuedojo xhrPost 问题
【发布时间】:2012-06-06 10:09:46
【问题描述】:

这是我的源代码:

<script>
  function sendText(){
    require([ "dijit/form/Button", "dojo/_base/xhr"],
      function(Button,xhr) {

        xhr.post({
          url: "validURL1.html",
          form: dojo.byId("myForm"),
          load: function(data){
            var newStore = 
              new ItemFileWriteStore({url:'validURL2.html'});
            dijit.byId("grid").setStore(newStore);
          },
          error: function(error){
            alert("error!");
          }
        });
    });
  }
</script>

<button data-dojo-type='dijit.form.Button' onClick ='sendText()'>submit</button>

但是当我按下按钮并尝试将我的数据发布到服务器时,萤火虫说:

_145 未定义

那么我的代码有什么问题? 什么是错误'_145'?

更新

<script>

require([ "dijit/form/Button", "dojo/_base/xhr","dijit/form/Form",  "dojo/data/ItemFileWriteStore", 
          "dojo/dom-form","dijit/registry","dojo/ready", "dojox/grid/EnhancedGrid"],
        function(Button,xhr, Form, ItemFileWriteStore, domForm, registry,ready, EnhancedGrid) {
        var hasBeenSent = false;

        window.sendText = function() {

        xhr.post({
            url: "validURL1.html",
            form: dojo.byId("myForm"),
            handleaAs: "text",
            load: function(data) {
                var newStore = new ItemFileWriteStore({url:'validURL2.html'});
                dojo.byId("grid").setStore(newStore);
          },
          error: function(error){

            alert("error!");
          },
          handle: function() {

                hasBeenSent = true;
            }
        });

            }   
});
</script>

现在它说:

TypeError: dojo.byId("grid").setStore is not a function

但是,我需要“enhancedGrid”。所以也许我应该需要一些其他的模块或类?

【问题讨论】:

  • 我不完全确定,但我认为如果你同时设置了loadhandle,那么只有一个会运行

标签: javascript ajax dojo xmlhttprequest


【解决方案1】:

将 dojo.byId("grid") 更改为 dijit.byId("grid") 因为您对 dojo.byId("grid") 的调用只会返回 DOMNode 而不是 Widget。

此外,请确保如果您的“网格”是标记声明,则 dojo.parser.parse() 已运行。如果 parseOnLoad:true 已设置,您需要等待 dojo.ready 触发,例如dojo.ready(function() { require.... });require(["dojo/domReady!", ....], function(..) { XHR });

如果只在你的 require 语句中调用 update-xhr,这个结构最终会表现得更好。

require([
     "dojo/parser", // Pull in parser to manually run it if parseOnLoad is not set true
     "dijit/form/Button",
     "dojo/_base/xhr",
     ...
     "dojox/grid/EnhancedGrid",
     "dojo/domReady!" // Wait untill DOM is done loading and all of the dojo base has been prepared
   ], function(
      Parser,
      Button,
      ...
   ) {
        Parser.parse();
        var hasBeenSent = false;

        window.sendText = function() {

        xhr.post({
            url: "sample/update.html",
            form: dojo.byId("updateUser"),
            handleaAs: "text",
            load: function(data) {
                var newStore = new ItemFileWriteStore({url:'sample/userLissts.html'});
                dijit.byId("grid").setStore(newStore);
          },
          error: function(error){

            alert("error!");
          },
          handle: function() {

                hasBeenSent = true;
            }
        });

            }   
});

【讨论】:

  • @mschr 我忘了回答我的问题,但你的回答是正确的
【解决方案2】:

您正在使用压缩/缩小版的 dojo。执行此操作的算法会将变量名替换为较小的变量名(即 _145)以减小 javascript 文件的大小。

查看压缩的dojo文件,发现如下:

function formToObject(_145){var ret={},_146=dom.byId(_145).elements;

我猜dojo.byId("myForm") 没有返回您的表单。

我还建议您将开发环境设置为能够使用未压缩的文件。它将允许在浏览器中进行更好的调试。

http://swingingcode.blogspot.com/2012/03/dojo-configurations.html

【讨论】:

    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多