【问题标题】:dojo xhr request new syntax doesn't load data as expecteddojo xhr 请求新语法未按预期加载数据
【发布时间】:2017-08-08 20:37:00
【问题描述】:

正如标题所说,我正在为 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: Unexpected token o 在 Object.parse (本机) 在 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 [作为句柄响应] (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测试过,还是不行。

我不知道有什么问题。我的代码有问题还是其他问题?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript json dojo xmlhttprequest dojo.xhrget


    【解决方案1】:

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

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

    尝试将您的一个 JSON 响应粘贴到 http://jsonlint.org/ - 如果它没有在那里验证,那么它将无法使用 dojo/request 验证。

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

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

    【讨论】:

    • 问题是为什么旧语法与我的 json 数据完美配合,而不是新语法?我已经将我的 json 传递给 jsonlint.orgjsonformat.com,但它没有经过验证,因为它缺少包含在键内的双引号,但我认为这不是问题,因为即使我更正了它,还是不行。
    • 您能否编辑您的问题以包含更正后的 JSON(如果它很长,则为它的子集)?无效的 JSON 可能是问题所在,因为正如我所解释的,旧 API 由于其处理数据的方式而更加宽容。鉴于您的场景中的其他一切都保持不变,这是唯一容易想到的事情。
    • 感谢您的回复,我按照您的要求包含了我的 json 数据的子集。
    • 我使用您的 JSON 进行了测试,新旧 API 似乎都适合我。 gist.github.com/kfranqueiro/f89ffeb1f41ace33ea68
    • 感谢您的测试,这个有效的 JSON 也适用于我,但问题在于旧的 JSON 适用于旧语法,但不适用于新语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2017-11-05
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多