如何检索AMD的Dojo的XHR响应代码(+时间戳)? [英] How to retrieve XHR response code (+timestamp) of AMD'ized Dojo?

查看:128
本文介绍了如何检索AMD的Dojo的XHR响应代码(+时间戳)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用旧Dojo可以将第二个参数 ioargs 传递给Xhr请求的 load 函数(请参阅此处的示例6)。这个 ioargs 提供了(除其他外)请求的时间戳和状态代码。

With the "old" Dojo one could pass a second argument ioargs to the load function of a Xhr request (see Example 6 here). This ioargs provided (among other things) the timestamp and status code of the request.

但是如何实现这与新的和更清洁(和向前兼容)Dojo?

不幸的是,我在
当前文档

But how can I achieve this with the new and "cleaner" (and forward compatible) Dojo?
Unfortunately, I could not find any hints in the current documentation.

以下应该是上面引用的例子的端口到新的Dojo。但是, ioargs 将是未定义的:

The following should be a port of above referenced example to the "new" Dojo. But, ioargs will be undefined:

require( "dojo/request/xhr", "dojo/dom", "dojo/domReady!",
  function(request, dom){
    // Look up the node we'll stick the text under.
    var targetNode = dom.byId("getLicenseStatus");

    // The parameters to pass to xhrGet, the url, how to handle it, and the callbacks.
    request.get(
      "{{dataUrl}}dojo/LICENSE",
      {
        handleAs: "text",
        preventCache: true
      }
    ).then(
      function(data, ioargs){
        // FIXME: ioargs is undefined
        targetNode.innerHTML = "XHR returned HTTP status: " + ioargs.xhr.status;
      },
      function(error){
        targetNode.innerHTML = "An unexpected error occurred: " + error.response.status + ": " + error.response.text;
      }
    );
  }
);

需要更改什么才能在加载功能中使请求的时间戳和状态代码可用? / p>

What do I need to change to have the request's timestamp and status code available in the load function?

推荐答案

请求返回一个特殊的 promise 来源):


从dojo /请求呼叫返回的承诺有一个额外的属性不可用于标准承诺:响应。该属性是一个承诺,将解决一个冻结的对象(如果可用)更详细地描述响应:

Promises returned from dojo/request calls have an additional property not available on standard promises: response. This property is a promise that will resolve to a frozen object (where available) describing the response in more detail:


  • url - 最终使用的URL (请附带查询字符串)

  • 选项 - 用于进行请求的选项对象

  • text - 文本中的数据的字符串表示形式回复

  • 数据 - 响应中处理的数据(如果指定了句柄)

  • getHeader(headerName) - 一个函数,用于从请求;如果提供者不提供标题信息,则此函数将返回null。

  • url – final URL used to make the request (with query string appended)
  • options – options object used to make the request
  • text – string representation of the data in the response
  • data – the handled data in the response (if handleAs was specified)
  • getHeader(headerName) – a function to get a header from the request; if a provider doesn’t provide header information, this function will return null.

所以你应该链接 .then promise.response 以访问所有上述属性:

So you should chain .then to this promise.response to get access to all the aforementioned properties:

var promise = request.get("{{dataUrl}}dojo/LICENSE");

promise.response.then(function(response) {
    console.log("status", response.status);
    console.log("url", response.url);
    console.log("data", response.data);
});

查看jsFiddle中的一个工作示例: http://jsfiddle.net/phusick/6wB2L/

See a working example at jsFiddle: http://jsfiddle.net/phusick/6wB2L/

这篇关于如何检索AMD的Dojo的XHR响应代码(+时间戳)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆