0

在 rails 应用程序中,使用此文件:

配置/初始化程序/cors.rb

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
    headers: :any,
    methods: [:get, :post, :put, :patch, :delete, :options, :head],
    expose: ['X-Total-Count']
  end
end

这个后端在http://localhost:3001运行。

在前端,在react-admin源代码中:

src/App.js

import React from 'react';
import { Admin, Resource } from 'react-admin';
import jsonServerProvider from 'ra-data-json-server';
import { PostList } from './posts';

const dataProvider = jsonServerProvider('http://localhost:3001');

const App = () => (
  <Admin dataProvider={dataProvider}>
    <Resource name="posts" list={PostList} />
  </Admin>
);

export default App;

它在http://localhost:3000运行。

访问前端http://localhost:3000/#/posts时,chrome 浏览器控制台报错:

Warning: Missing translation for key: "The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider 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 X-Total-Count in the Access-Control-Expose-Headers header?"

即使设置expose: ['X-Total-Count']在后端,它也说失踪。为什么?

4

1 回答 1

1

查看有关数据提供者的文档。 https://marmelab.com/admin-on-rest//RestClients.html

jsonServerProvider您的 rails 可能与您尝试使用的提供程序不兼容。jsonServerProvider 不适合生产使用。它更像是 json-server 兼容的假休息端点的测试/示例提供程序。

如果其中一个预先构建的提供程序不适合您的需求,您将需要编写自己的传输。这很容易。

于 2018-07-06T17:02:14.633 回答