javascript - HTTP 响应中缺少 react-admin

标签 javascript reactjs spring cors react-admin

我正在尝试使用 react-admin 创建管理面板.

这是我所做的:

class AdminPanel extends React.Component{

    render() {
        return(
          <div>
              <Admin dataProvider={restProvider('http://localhost:8080')}>
                  <Resource name="u/all" list={PostList}/>
              </Admin>
          </div>
        );
    }
}

在另一个js文件中,我定义了PostList

在我的服务器中,我正在调试 /u/all 是否受到任何打击。越来越多了。

但是react.js给了我错误,说:错误:HTTP响应中缺少Content-Range header 。简单的 REST 数据提供程序期望资源列表的响应包含此 header 以及构建分页的结果总数。如果您使用 CORS,您是否在 Access-Control-Expose-Headers header 中声明了 Content-Range?

我该如何解决这个问题?

PostList.js:


export const PostList = (props) => (

    <List {...props}>
        <Datagrid>
            <TextField source="userID" />
            <TextField source="username" />
            <DateField source="firstName" />
            <TextField source="middleName" />
            <TextField source="lastName" />
            <TextField source="profileImageURL" />
            <TextField source="joiningTime" />
            <EditButton basePath="/posts" />
        </Datagrid>
    </List>
);

我的服务器端 CORS:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry
                        .addMapping("/**")
                        .allowedOrigins("http://localhost:3000","http://localhost:3001")
                        .exposedHeaders("Content-Range");
            }
        };
    }

enter image description here

添加公开的 header 后仍然出现错误:

enter image description here

最佳答案

答案写着in the react-admin documentation :

The simple REST client expects the API to include a Content-Range header in the response to getList calls. The value must be the total number of resources in the collection. This allows react-admin to know how many pages of resources there are in total, and build the pagination controls.

Content-Range: posts 0-24/319

If your API is on another domain as the JS code, you’ll need to whitelist this header with an Access-Control-Expose-Headers CORS header.

Access-Control-Expose-Headers: Content-Range

关于javascript - HTTP 响应中缺少 react-admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59876500/

相关文章:

javascript - 将 $http 与 Bootstrap UI 一起使用

javascript - jQuery 和 "Organized Code"

javascript - 将参数与字段数据一起传递给 onChange 事件上的函数

javascript - 带有 jQ​​uery each() 的 Slick Slider 背景图像循环

javascript - 如何在 Vue.js 中传递动态页面 :id to $http. 获取 url

reactjs - `getStaticProps` 填充页眉/页脚

mysql - 如何在 React 中渲染数组中的图像?

java - Spring安全配置来验证LDAP用户

java - 如何在 Spring Boot 中用 yml 文件替换属性文件

spring - JSF 2 托管 bean 及其在 EL 表达式中使用时的方法命名约定(与 Spring 结合使用)