1

我在任何地方都找不到这个,我不知道为什么 dojo 不能解决跨域 REST 请求。无论如何,这是问题所在:

我正在实现 dojo 数据网格,并且我试图从不在我的域中的 WCF 获取网格的数据,所以我提出了跨域问题,我试图通过使用 JSONP 来解决这个问题。

但我是道场的新手,所以我可能做错了什么。这是我的代码:

require([
         "dojo/store/JsonRest",
         "dojo/store/Memory",
         "dojo/store/Cache",
         "dojox/grid/DataGrid",
         "dojo/data/ObjectStore",
         "dojo/query",
         "dijit/form/Button",
         "dojo/domReady!",
         "dojo/request/script","dojo/dom-construct"
     ],function(script, domConstruct){

  //make the request just as before
  script.get("http://localhost:8060/ListService.svc/LoadLists?uid=c4446476-15e6-e111-9ecb-b7c5971d170a", {
    jsonp: "callback",
    query: {q: "#dojo"}
   }).then(function(data){
        test = data;         
    }).then(function(){
     console.log(results);
    });
 }, function (JsonRest, Memory, Cache, DataGrid, ObjectStore ,query) {

         grid = new DataGrid({
             store: dataStore = test,

             structure: [
                 { name: "Blog Id", field: "id", width: "50px", },
                 { name: "Name", field: "listtype", width: "200px",classes:"Name" },
                 { name: "Phone Number", field: "longlevel", width: "200px",classes:"test" }
             ]
         }, "gridTest"); // make sure you have a target HTML element with this id

         grid.startup();

         dojo.query("body").addClass("claro");

         grid.canSort = function () { return false; };


     });

我得到的错误是查询不是函数。任何想法如何正确实施。

4

1 回答 1

0

简单的错误只是将数据网格命令放在第一个然后返回数据

require([
         "dojo/store/JsonRest",
         "dojo/store/Memory",
         "dojo/store/Cache",
         "dojox/grid/DataGrid",
         "dojo/data/ObjectStore",
         "dojo/query",
         "dijit/form/Button",
         "dojo/domReady!",
         "dojo/request/script","dojo/dom-construct"
     ],function(script, domConstruct){

  //make the request just as before
  script.get("http://localhost:8060/ListService.svc/LoadLists?uid=c4446476-15e6-e111-9ecb-b7c5971d170a", {
    jsonp: "callback",
    query: {q: "#dojo"}
   }).then(function(data){
       function (JsonRest, Memory, Cache, DataGrid, ObjectStore ,query) {

         grid = new DataGrid({
             store: dataStore = test,

             structure: [
                 { name: "Blog Id", field: "id", width: "50px", },
                 { name: "Name", field: "listtype", width: "200px",classes:"Name" },
                 { name: "Phone Number", field: "longlevel", width: "200px",classes:"test" }
             ]
         }, "gridTest"); // make sure you have a target HTML element with this id

         grid.startup();

         dojo.query("body").addClass("claro");

         grid.canSort = function () { return false; };


     })        
    }).then(function(){
     console.log(results);
    });
 };

如果您仍然发现问题可能是因为在请求从 wcf 返回之前创建了网格。

如果发生这种情况,请使用 dojo 1.8 中的 Deferred,它可以让您控制您的流程顺序。

因此您将能够调用请求给它一些时间,然后将数据放入要查看的网格中。

于 2012-09-02T17:47:28.217 回答