Ruby on rails 无法从react admin访问rails开发的API
在rails应用程序中,使用以下文件: config/initializers/cors.rbRuby on rails 无法从react admin访问rails开发的API,ruby-on-rails,api,header,cors,react-admin,Ruby On Rails,Api,Header,Cors,React Admin,在rails应用程序中,使用以下文件: config/initializers/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],
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
此后端在上运行
在前端,在react管理源中:
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;
即使在后端设置expose:['X-Total-Count'],它也表示缺少。为什么?请查看有关数据提供程序的文档。 您的rails可能与您尝试使用的jsonServerProvider不兼容。jsonServerProvider不适用于生产使用。它更像是json服务器兼容的伪rest端点的测试/示例提供程序 如果其中一个预构建的提供者不能满足您的需要,您将需要编写自己的传输。这很容易
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?"