How to make dojo.request.xhr GET request with basic authentication

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

How and where can I specify the username and password in the Dojo XHR settings?

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
  });
});

Thanks.

+4
source share
1 answer

In fact, you should be able to pass the user and password using the properties userand passwordin the object options.

In previous versions of Dojo, this has been documented, but it seems that now it is not. However, I just tested it and seemed to add a username and password to the url, like this:

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

URL-, .


, , :

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

dojox/encoding/base64 :

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

Source: https://habr.com/ru/post/1529080/


All Articles