dojo xhr请求新语法不按预期加载数据

时间:2015-04-17 15:44:47

标签: javascript json dojo xmlhttprequest dojo.xhrget

就像标题所说我正在为 dojo/request/xhr 使用新的dojo语法,但这似乎不起作用,并在加载数据时抛出错误,而旧的语法用相同的url给出想要的结果。

这是当前没有正确加载数据的语法:

    this.get = function (url, loadFunc, errorFunc, handleFunc) {
        xhr( url, {
            handleAs: 'json'
        }).then(function(data){
            typeof loadFunc === 'function' ? loadFunc : function () { };
            console.log(url+ " load");
            console.log(data);
        }, function(error){
            typeof errorFunc === 'function' ? errorFunc : function () {  };
            console.log(url+" error");
            console.dir(error);
        }, function(handle){
            typeof handleFunc === 'function' ? handleFunc : function () { };
            console.log(url+" handle");
            console.log(handle);
        });
    };

正如您所看到的,我正在将数据打印到控制台,我正在获取正确的数据,但xhr请求会抛出此错误:

  

" SyntaxError:意外的令牌o       at Object.parse(native)       在l.json(http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:228:250)       在m(http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:227:277)       在j [as handleResponse](http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:151:351)       在XMLHttpRequest.e(http://ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js:154:393)"

虽然以下旧语法完美无缺:

    this.get = function (url, loadFunc, errorFunc, handleFunc) {
      dojo.xhrGet({
            url: url,
            handleAs: 'json',
            load: typeof loadFunc === 'function' ? loadFunc : function () { },
            error: typeof errorFunc === 'function' ? errorFunc : function () { },
            handle: typeof handleFunc === 'function' ? handleFunc : function () { }
        });
    };

修改

这是我的JSON数据:

{ "assistants" : [ { "assistants" : [ "M 11",
        "M 1"
      ],
    "name" : "M X1"
  },
  { "assistants" : [ "M 2",
        "M 2XX1",
        "M 3"
      ],
    "name" : "M 1"
  },
  { "assistants" : [ "M 2" ],
    "name" : "M 2"
  },
  { "assistants" : [  ],
    "name" : "M 3"
  }
],
"chiefs" : [ { "chief" : "M X1",
    "name" : "M 11"
  },
  { "chief" : "M 11",
    "name" : "M 1"
  },
  { "chief" : "M X1",
    "name" : "M 2"
  },
  { "chief" : "M 744X1",
    "name" : "M 3"
  }
],
"departments" : [ { "assistants" : [ "M 11",
        "M 3",
        "M 21"
      ],
    "chief" : "M X1",
    "email" : "dg@somedomain.com",
    "interim" : [ "M X542",
        "M 4"
      ],
    "members" : [ "M 2",
        "M 3",
        "M 4",
        "M 5",
        "M X24544"
      ],
    "name" : "Dep1",
    "notify" : [ "Dep2",
        "M X2",
        "M 21"
      ],
    "resp" : "M 21",
    "validators" : [ "Dep2",
        "M 2",
        "M 558"
      ]
  },
  { "chief" : "M 1",
    "email" : "admin@somedomain.com",
    "members" : [ "M 11",
        "M 12"
      ],
    "name" : "Dep3",
    "parent" : "Dep1"
  },
  { "chief" : "M 11",
    "email" : "commercial@somedomain.com",
    "members" : [ "M 21",
        "M 22"
      ],
    "name" : "Dep4",
    "parent" : "Dep1"
  }
],
 "orgaTestModel" : { "corporation" : "Corporation Sample",
  "name" : "Orga Sample 7855"
},
"root" : "Dep1"
}

注意: 我使用的是dojo 1.8.1版本,但我也使用dojo 1.9.2进行了测试,但它仍然无效。

我无法弄清问题是什么。我的代码有问题还是另一个问题?

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您还没有提供原始JSON响应的示例,但我猜它是无效的JSON。

dojo.xhrGet设置为eval时,

handleAs实际上使用"json",根据定义,它将比严格的JSON解析器更宽松。另一方面,dojo/request使用JSON.parse(如果可用),它期望JSON格式正确(例如,所有键都是带引号的字符串,所有字符串都使用双引号)。

尝试将您的一个JSON回复粘贴到http://jsonlint.org/ - 如果它没有在那里验证,那么它就不会使用dojo/request进行验证。

dojo.xhrGet使用eval的原因,即使它可能不安全,是因为它是在广泛的JSON.parse支持之前编写的,并且更改它会导致倒退兼容性 - 换句话说,即使没有切换API,开发人员也会遇到你现在遇到的问题。)

编辑:现在问题中提供的JSON是有效的,并且适用于旧API和新API。