【问题标题】:Response for preflight is invalid (redirect) on ReactJSReactJS 上的预检响应无效(重定向)
【发布时间】:2018-07-04 12:39:26
【问题描述】:

我有一个受 azure ad 保护的 reactjs 应用程序,还使用了一个也受 azure ad 保护的 WEB API。

我基本上想测试一个返回一些字符串的简单组件,看看整个数据流是否正常工作。

我的反应组件是这样的:

import React, { Component } from 'react';

import { Row, Col } from 'antd';
import PageHeader from '../../components/utility/pageHeader';
import Box from '../../components/utility/box';
import LayoutWrapper from '../../components/utility/layoutWrapper.js';
import ContentHolder from '../../components/utility/contentHolder';
import basicStyle from '../../settings/basicStyle';
import IntlMessages from '../../components/utility/intlMessages';
import { adalApiFetch } from '../../adalConfig';

export default class extends Component {
  constructor(props) {
    super(props);
    this.state = {
        data: []
    };

  }

  getValues() {
    adalApiFetch(fetch, '/values', {})
      .then((response) => {

        // This is where you deal with your API response. In this case, we            
        // interpret the response as JSON, and then call `setState` with the
        // pretty-printed JSON-stringified object.
        response.json()
          .then((responseJson) => {
            this.setState({ data: JSON.stringify(responseJson, null, 2) })
          });
      })
      .catch((error) => {

        // Don't forget to handle errors!
        console.error(error);
      })

  }

  fetchData() {
    try {
        const data =  this.getValues();
        !this.isCancelled && this.setState({ data });
    } catch(error) {
        console.log(error);
    }
  }

  componentDidMount(){
    this.fetchData();
  }

  render() {
    const { data } = this.state;
    const { rowStyle, colStyle, gutter } = basicStyle;
    const radioStyle = {
        display: 'block',
        height: '30px',
        lineHeight: '30px'
      };
      const plainOptions = ['Apple', 'Pear', 'Orange'];
      const options = [
        { label: 'Apple', value: 'Apple' },
        { label: 'Pear', value: 'Pear' },
        { label: 'Orange', value: 'Orange' }
      ];
      const optionsWithDisabled = [
        { label: 'Apple', value: 'Apple' },
        { label: 'Pear', value: 'Pear' },
        { label: 'Orange', value: 'Orange', disabled: false }
      ];

    return (
      <div>
        <LayoutWrapper>
        <PageHeader>{<IntlMessages id="pageTitles.TenantAdministration" />}</PageHeader>
        <Row style={rowStyle} gutter={gutter} justify="start">
          <Col md={12} sm={12} xs={24} style={colStyle}>
            <Box
              title={<IntlMessages id="pageTitles.TenantAdministration" />}
              subtitle={<IntlMessages id="pageTitles.TenantAdministration" />}
            >
              <ContentHolder>
                  <ul>
                    {data && data.map(item => (
                        <li>{item.name}</li>
                    ))}
                  </ul>
              </ContentHolder>
            </Box>
          </Col>
        </Row>
      </LayoutWrapper>
      </div>
    );
  }
}

我的adalconfig是这样的,替换了真正的guid:

import { AuthenticationContext, adalFetch, withAdalLogin } from 'react-adal';

export const adalConfig = {
  tenant: 'a-c220-48a2-a73f-1177fa2c098e',
  clientId: 'a-bd54-456d-8aa7-f8cab3147fd2',
  endpoints: {
    api:'a-abaa-4519-82cf-e9d022b87536'
  },
  'apiUrl': 'https://a-app.azurewebsites.net/api',
  cacheLocation: 'localStorage'
};

export const authContext = new AuthenticationContext(adalConfig);

export const adalApiFetch = (fetch, url, options) =>
  adalFetch(authContext, adalConfig.endpoints.api, fetch, adalConfig.apiUrl+url, options);

export const withAdalLoginApi = withAdalLogin(authContext, adalConfig.endpoints.api);

但是在测试时我在控制台中出现了这个错误:

Failed to load https://a-app.azurewebsites.net/api/values: Response for preflight is invalid (redirect)

UI 应用程序和 web api 应用程序已在 azure AD 上注册,并且在 webapi 清单中我对其进行了编辑以允许隐式授予并添加 knownClientApplications 值以匹配 UI 应用程序注册 ID。

我还在 azure AD 上授予了客户端应用程序对 Web api 应用程序的权限。

【问题讨论】:

  • 难道不是CORS 问题服务器端吗?
  • 我检查了webapi上没有启用cors
  • 那么它需要被启用。预检是 CORS 要求的一部分,不能重定向,必须返回 CORS 标头
  • 将其添加为答案 :)

标签: javascript reactjs azure-active-directory adal adal.js


【解决方案1】:

我用 * 启用了 cors,就是这样。

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2016-10-20
    • 2017-04-25
    • 2016-06-20
    • 2017-11-15
    • 2017-12-30
    • 2019-03-21
    • 2018-02-13
    • 2018-08-25
    相关资源
    最近更新 更多