如何使用基本身份验证发出dojo.request.xhr GET请求 [英] How to make dojo.request.xhr GET request with basic authentication

查看:109
本文介绍了如何使用基本身份验证发出dojo.request.xhr GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 Dojo v的文档.1.9 request / xhr
,我找不到包含基本身份验证的示例。

I look at the documentation for Dojo v.1.9 request/xhr and I cannot find example that includes basic authentication.

我如何以及在何处将用户名和密码包括在其中Dojo XHR选项?

How and where do I include the User name and Password in the Dojo XHR options?

require(["dojo/request/xhr"], function(xhr){
  xhr("example.json", {
    // Include User and Password options here ?
    user: "userLogin"
    password: "userPassword"
    handleAs: "json"
  }).then(function(data){
    // Do something with the handled data
  }, function(err){
    // Handle the error condition
  }, function(evt){
    // Handle a progress event from the request if the
    // browser supports XHR2
  });
});

谢谢。

推荐答案

确实,您应该能够使用 user password 属性传递用户名和密码在 options 对象中。

Indeed, you should be able to pass the user and password with the user and password property in the options object.

在Dojo的早期版本中已对此进行了记录,但现在看来它们不是 t。但是,我刚刚对其进行了测试,它似乎在URL中添加了用户名和密码,例如:

In previous versions of Dojo this was documented, but it seems that now they aren't. However, I just tested it and it seems to add the username and password to the URL, like:

http://user:password@myUrl/example.json

通常,浏览器应该能够翻译此URL,因此请求

Normally the browser should be capable of translating this URL so the request headers are set.

您也可以手动设置这些标题,例如使用:

You could also set these headers manually, for example by using:

xhr("example.json", {
    headers: {
        "Authorization": "Basic " + base64.encode(toByteArray(user + ":" + pass))
    }
}).then(function(data) {
    // Do something 
});

但是,这需要 dojox / encoding / base64 模块和以下函数:

However, this requires the dojox/encoding/base64 module and the following function:

var toByteArray = function(str) {
    var bytes = [];
    for (var i = 0; i < str.length; ++i) {
        bytes.push(str.charCodeAt(i));
    }
    return bytes;
};

这篇关于如何使用基本身份验证发出dojo.request.xhr GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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