使用同构获取的基本身份验证

Basic auth with isomorphic-fetch

我想做一个 post 请求,使用同构获取传递一些凭据。这是我现在的代码:

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

此代码无效,因为它重定向到登录页面。关于如何使这项工作有任何想法吗?

Basic auth with isomorphic-fetch

您的代码就可以了。也就是说,它将 post 授权 header 就好了。

更多

重定向逻辑很可能已损坏。