【问题标题】:Error: The Content-Range header is missing in the HTTP Response错误:HTTP 响应中缺少 Content-Range 标头
【发布时间】:2018-04-25 00:30:33
【问题描述】:

我在休息时设置管理员,现在当我尝试获取数据时出现此错误,即使我从服务器收到所有需要的数据:

The Content-Range header is missing in the HTTP Response. The simple REST client expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?

有没有办法在不更改 API 的情况下解决它?我正在根据教程进行授权,这是我的app.js:

   if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    const token = localStorage.getItem('token');
    options.headers.set('Authorization', `Bearer  ${"token"}`);
    return fetchUtils.fetchJson(url, options);
}
const restClient = simpleRestClient('https://mywebsite.com', httpClient);

const App = () => (
<Admin restClient={restClient} authClient={authClient}>
    <Resource name="posts"  list={PostList}  edit={PostEdit} create={PostCreate}/>
        <Resource name="users" list={UserList}/>
    </Admin>
);

【问题讨论】:

  • 是的,但最好的答案是在rails内部解决它response.headers['X-Total-Count'] = '30' response.headers['Access-Control-Expose-Headers'] = 'X-Total-Count'我已经尝试过了,它不起作用。有没有办法在 reactjs 里面解决?
  • 尝试Content-Range 而不是X-Total-Count。
  • 这几行给我一个错误ArgumentError (wrong number of arguments (given 0, expected 2)):
  • response.headers['Content-Range'] = 'posts 0-24/319'

标签: javascript ruby-on-rails reactjs http-headers admin-on-rest


【解决方案1】:

问题不在于 React-App,而在于您的 REST 服务器。

就我而言,我使用的是 SimpleRestClient,并且在他们的文档中显示

import simpleRestProvider from 'ra-data-simple-rest';

注意:简单的 REST 客户端期望 API 包含一个 对 GET_LIST 调用的响应中的 Content-Range 标头。值必须 是集合中的资源总数。这允许 admin-on-rest 知道总共有多少页资源, 并构建分页控件。

Content-Range:posts 0-24/319 如果您的 API 在另一个域上 JS 代码,您需要将此标头列入白名单 Access-Control-Expose-Headers CORS 标头。

Access-Control-Expose-Headers:内容范围

因此,它必须从您的服务器/REST 服务器返回(包括响应)两个标头

  1. 访问控制公开标头:内容范围
  2. 内容范围:帖子 0-24/319

在我的烧瓶服务器中,这就是我所做的

  1. 在您的回复中添加“内容范围”标题。

    response.headers.add('Access-Control-Expose-Headers', 'Content-Range')

  2. 添加标题'Content-Range'并为其分配一个范围值(通常以字节为单位)

    response.headers.add('Content-Range','bytes : 0-9/*')

最后:我注意到,当您的响应中省略任何一个标题时,您会收到相同的错误

错误:HTTP 响应中缺少 Content-Range 标头

确保您的服务器返回这些标头

'Access-Control-Expose-Headers'、'Content-Range' 要么 '内容范围','字节数:0-9/*'

我希望这会有所帮助,因为这是我第一次回答 SO 问题

【讨论】:

    【解决方案2】:

    如果您在 react admin 中使用 fastapi,则需要将其添加到路由

       response.headers['X-Total-Count'] = '30' 
       response.headers['Access-Control-Expose-Headers'] = 'Content-Range'
    ``
    

    【讨论】:

      【解决方案3】:

      您需要为您的请求添加自定义标头,您可以将 fetchJson() 调用包装在您自己的函数中:

      例如:

      import { fetchUtils, Admin, Resource } from 'react-admin';
      import simpleRestProvider from 'ra-data-simple-rest';
      
      const fetchJson = (url, options = {}) => {
          if (!options.headers) {
              options.headers = new Headers({ Accept: 'application/json' });
          }
          // add your own headers here
          options.headers.set('X-Custom-Header', 'foobar');
          return fetchUtils.fetchJson(url, options);
      }
      const dataProvider = simpleRestProvider('http://path.to.my.api/', fetchJson);
      
      const App = () => (
          <Admin dataProvider={dataProvider}>
              <Resource name="posts" list={PostList} />
          </Admin>
      );  
      

      自定义标头最常见的用途是用于身份验证。 fetchJson 内置了对 Authorization 令牌头的支持:

      const fetchJson = (url, options = {}) => {
          options.user = {
              authenticated: true,
              token: 'SRTRDFVESGNJYTUKTYTHRG'
          };
          return fetchUtils.fetchJson(url, options);
      };
      const dataProvider = simpleRestProvider('http://path.to.my.api/', fetchJson);
      

      现在对 REST API 的所有请求都将包含 Authorization: SRTRDFVESGNJYTUKTYTHRG 标头。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        相关资源
        最近更新 更多