Javascript Dojo JsonRest基本身份验证

Javascript Dojo JsonRest基本身份验证,javascript,rest,dojo,basic-authentication,Javascript,Rest,Dojo,Basic Authentication,我想使用Dojo JsonRest从REST服务读取JSON数据。REST服务需要用户名和密码作为基本身份验证字符串。首先,我硬编码了这个字符串。现在,我尝试了以下方法: var processStore = new JsonRest({ target: "http://host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", allowNoTrailingSlas

我想使用Dojo JsonRest从REST服务读取JSON数据。REST服务需要用户名和密码作为基本身份验证字符串。首先,我硬编码了这个字符串。现在,我尝试了以下方法:

    var processStore = new JsonRest({
    target: "http://host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
    allowNoTrailingSlash: false,
    user: "test",
    password: "test"
    });

但这并不奏效。因此,我的问题是:如何使用Dojo JsonRest发送基本身份验证凭据?

您可以尝试以下两种方法之一:

1) 将用户名和密码放入目标url本身,如下所示:

var processStore = new JsonRest({
    target: "http://username:password@host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
    allowNoTrailingSlash: false
});
如果用户名是电子邮件地址,则:

var processStore = new JsonRest({
    target: "http://useremail%40whatever.com:password@host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
    allowNoTrailingSlash: false
});
2) 使用JsonRest的头属性:

var encodedLogin = "Basic " + window.btoa("username:password");
var processStore = new JsonRest({
    target: "http://host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
    allowNoTrailingSlash: false,
    headers: {
        Authorization: encodedLogin
    }
});

此解决方案对用户名和密码组合字符串使用base64编码,然后将其与“Authorization”头参数一起发送。

您可以将用户名和密码放入目标url中,如下所示:“”。使用用户名:password@domain.com之后,http://可以工作。此外,如果用户名是电子邮件地址,则在将地址中的@符号放入链接之前,应首先将其替换为“%40”:myemail%40where.com:password@domain.com