我想做一个使用同构获取传递一些凭据的发布请求。这是我现在拥有的代码:
export default function fetchJson(url: string, options: any) {
options.headers = (<any>Object).assign({
'Accept': 'application/json',
'Content-Type': 'application/json'
}, options.headers);
return fetch(url, options)
.then(checkStatus)
.then(parseJSON);
}
export function fetchLogs(dispatch_ok: any, dispatch_error: any) {
var URL = "http://server_address:8080/api/getlogs/";
return fetchJson(URL, {
method: 'get',
credentials: 'include',
headers: {
Authorization: 'Basic '+window.btoa('user:passwd'),
}
}).then((data: any) => {
if (data.results.length != 0) {
dispatch_ok(data.results);
} else {
dispatch_error("No logs could be retrieved");
}
}).catch((error: any) => {
dispatch_error(error.message);
});
}
此代码不起作用,因为它重定向到登录页面。关于如何使这项工作的任何想法?