【问题标题】:Getting the React CORS preflight error in React Native在 React Native 中获取 React CORS 预检错误
【发布时间】:2021-11-09 23:48:01
【问题描述】:

连接到外部 API 时,我的 React Native 应用程序出现 CORS 访问错误。

async componentDidMount() {
// POST request using fetch with async/await
const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: JSON.stringify({ type:'accountLookup',storeId:STORE_ID, storeKey:STORE_KEY, memberId:'471324' })
};
const response = await fetch('http://frequent5.revlogical.com/wsMobile', requestOptions);
const data = await response.json();
this.setState({ data: data });

【问题讨论】:

    标签: reactjs react-native cors


    【解决方案1】:

    问题很可能不在于 React 应用程序,而在于您的服务器未配置为为您的应用程序提供服务。 您应该在服务器上设置“Access-Control-Allow-Origin”标头,以允许您的应用地址发出请求。

    【讨论】:

      【解决方案2】:

      这个问题通常是后端的问题。用https://www.test-cors.org/之类的工具测试一下

      另一种方法是在前端和API之间创建一个要拦截的服务器,您可以处理这家伙的cors

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2022-01-26
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 2017-10-05
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多