无法通过 XMLHttpRequest 从 Circuit 获取附件内容
Could not get attachments content from Circuit by XMLHttpRequest
我有一个附件列表 url,想一键下载。我已将 url 作为数组加载到 javascript 中。然后我使用 XMLHttpRequest 来获取文件内容。但是,似乎 header 或身份验证在发送到 Circuitsandbox(后来为了生产而发送到 Circuit)时不正确,我总是收到错误 401 或 404,即使 url 可以在浏览器上下载。
所以我的问题是 header/authentication 我应该为我的请求使用什么?
这是我的例子:
var auth = user + ':' + password;
var hash = Base64.encode(auth);
...
xhr.setRequestHeader("Authorization", "Basic " + hash );
我应该在这里使用我的 Circuitsandbox 用户(电子邮件)和密码吗?
非常感谢您的任何提示。
用于下载附件的电路文件 api 不支持基本身份验证。由于您在已经有会话的浏览器中 运行 this,因此您只需将 xhr.withCredentials 设置为 true。这将使用您当前会话的 cookie。参见 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
我刚刚创建了一个示例 jsbin,用于下载 post 的附件。只需输入 post 的 itemId。在Circuit webclientpost中右击时间戳可以看到itemId。
https://output.jsbin.com/napenuy
client.getItemById(itemId.value)
.then(item => {
console.log('Attachments:', item.attachments);
item.attachments.forEach(file => {
const xhr = new XMLHttpRequest();
xhr.open('GET', file.url, true);
xhr.withCredentials = true;
xhr.onload = function(e) {
if (this.status == 200) {
result.innerHTML += `<br>${file.fileName}:<br>${this.responseText}`;
}
}
xhr.send();
})
})
.catch(console.error);
我有一个附件列表 url,想一键下载。我已将 url 作为数组加载到 javascript 中。然后我使用 XMLHttpRequest 来获取文件内容。但是,似乎 header 或身份验证在发送到 Circuitsandbox(后来为了生产而发送到 Circuit)时不正确,我总是收到错误 401 或 404,即使 url 可以在浏览器上下载。
所以我的问题是 header/authentication 我应该为我的请求使用什么?
这是我的例子:
var auth = user + ':' + password;
var hash = Base64.encode(auth);
...
xhr.setRequestHeader("Authorization", "Basic " + hash );
我应该在这里使用我的 Circuitsandbox 用户(电子邮件)和密码吗?
非常感谢您的任何提示。
用于下载附件的电路文件 api 不支持基本身份验证。由于您在已经有会话的浏览器中 运行 this,因此您只需将 xhr.withCredentials 设置为 true。这将使用您当前会话的 cookie。参见 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
我刚刚创建了一个示例 jsbin,用于下载 post 的附件。只需输入 post 的 itemId。在Circuit webclientpost中右击时间戳可以看到itemId。
https://output.jsbin.com/napenuy
client.getItemById(itemId.value)
.then(item => {
console.log('Attachments:', item.attachments);
item.attachments.forEach(file => {
const xhr = new XMLHttpRequest();
xhr.open('GET', file.url, true);
xhr.withCredentials = true;
xhr.onload = function(e) {
if (this.status == 200) {
result.innerHTML += `<br>${file.fileName}:<br>${this.responseText}`;
}
}
xhr.send();
})
})
.catch(console.error);