找不到任何相关文档,因此在我深入研究代码之前,是否有人知道在使用“fetch”发出 REST 请求时如何使用基本身份验证(https://github.com/github/fetch https://github.com/github/fetch).
刚刚尝试了以下行,但请求中未设置标头:
fetch('http://localhost:8080/timeEntry', {
mode: 'no-cors',
headers: { 'Authorization': 'Basic YW5kcmVhczpzZWxlbndhbGw=' }
})
.then(checkStatus)
.then(parseJSON)
.then(function(activities) {
console.log('request succeeded with JSON response', data);
dispatch(activitiesFetched(activities, null));
}).catch(function(error) {
console.log('request failed', error);
dispatch(activitiesFetched(null, error));
});
用户名和密码是我自己的名字和姓氏,使用curl
有用。
如果我把{ 'Accept' : 'application/test' }
接受已设置,只是没有Authorization
... 奇怪的。
只是为了我能够继续我添加credentials: 'include'
这使得浏览器提示输入用于与 REST 后端通信的用户名和密码。仅用于测试,稍后将使用 OAuth。
fetch('http://localhost:8080/timeEntry', {
mode: 'no-cors',
credentials: 'include'
})
.then(checkStatus)
.then(parseJSON)
.then(function(activities) {
console.log('request succeeded with JSON response', data);
dispatch(activitiesFetched(activities, null));
}).catch(function(error) {
console.log('request failed', error);
dispatch(activitiesFetched(null, error));
});