【问题标题】:How to fix CORS error when running the React app on Android emulator?在 Android 模拟器上运行 React 应用程序时如何修复 CORS 错误?
【发布时间】:2021-05-25 11:30:14
【问题描述】:

我开发了一个Ionic React 应用程序,运行在Capacitor 之上。在浏览器(离子服务)中,应用程序运行完美。 当我在 Android emulator 上运行应用程序并尝试登录我的应用程序时,会显示 CORS 错误。 考虑到我无法更改 API 服务器后端,我该如何解决此错误。 在我的应用中,我使用 GET、POST 和 PUT 等不同方法联系外部 API。

浏览器使用 axios 的 POST 请求示例:

    const doLogin = (e: any) => {
    e.preventDefault();

    const data = {

      cardNr: cardNumber,
      name: name,
    };

    axios
    .post("/login", data)
    .then((response) => {
    //do sth
    }
    return response.data;
    })
    .catch((error) => {
    console.log(error);
    });

** 我无法在 API 后端进行更改。

我尝试了以下方法:

  1. 安装cordova-plugin-advanced-http,根据https://ionicframework.com/docs/native/http/:它要求安装@angular/core,但我正在使用React。无论如何,即使安装了它,我也无法在Android上成功运行该应用程序,无法登录。
import { HTTP } from '@ionic-native/http/ngx';

    const doLogin = (e: any) => {
    e.preventDefault();

    const data = {

      cardNr: cardNumber,
      name: name,
    };

var http = new HTTP();

    http
    .post("https://xx/login", data, {})
    .then((response) => {
    //do sth
    }
    return response.data;
    })
    .catch((error) => {
    console.log(error);
    });
  1. 安装@capacitor-community/http,根据https://github.com/capacitor-community/http:在这种情况下,当我导入‘import com.getcapacitor.plugin.http.Http’时会显示错误:‘Cannot resolve symbol http’。
import { Plugins } from "@capacitor/core";
 const doLogin = (e: any) => {
    e.preventDefault();

    const data = {

      cardNr: cardNumber,
      name: name,
    };

const { Http } = Plugins;
    await Http.request({
      method: "POST",
      url: "https://xx/login",
      data: data,
    })
    .then((response) => {
    //do sth
    }
    return response.data;
    })
    .catch((error) => {
    console.log(error);
    });

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: android reactjs ionic-framework capacitor capacitor-plugin


    【解决方案1】:

    一个快速的解决方法可能是将 post 请求的标头设置为不会触发 cors 请求的内容,例如"Content-Type": "application/x-www-form-urlencoded".

    这些请求称为“简单请求”,您可以阅读它们here。

    【讨论】:

    • 简单请求的唯一允许方法是:GET、HEAD、POST,除此之外,我也在我的应用程序中使用 PUT 请求。在这种情况下我该怎么办?
    • 在这种情况下,您可以在原始问题中添加更详细的问题描述,涵盖您正在联系的后端以及与您相关的方法。
    • 我编辑了我的问题。在后端我什么都做不了,我无权在那里更改任何内容。当我在 Android 上运行我的 react 应用程序时,问题是 CORS 错误。在浏览器中,我使用代理解决了它,但是当我在 Android 模拟器上运行我的应用程序时,这不是一个有效的解决方案。这就是为什么我尝试了其他选项,例如安装 http 插件,您可以在我的问题中阅读。
    • 未公开的api端点的CORS头告诉你什么?
    • 对不起,在哪里可以看到这个?
    【解决方案2】:

    有时服务器的所有者会在允许的来源中添加来源,以便只有这些来源可以调用 API,可能您的网站/本地 URL 与允许的来源不匹配,

    【讨论】:

    • 是的,它不匹配。但是,如果服务器的所有者出于安全原因不想允许 * Origins,我该怎么办?如何解决 Android 上的 CORS 错误?在浏览器中我使用代理解决了这个问题,但是这个解决方案对 Android 无效。
    • 如果服务器的所有者不允许,那么您就不能请求该服务器,
    【解决方案3】:

    对于任何感兴趣的人,我找到了解决方案。在此文档 https://github.com/capacitor-community/http 中,缺少 Android 配置。

    1- 所以,在 android\app\src\main\java\MainActivity.java 中导入并添加 http 插件

    package com.myapp.com;
    
    import android.os.Bundle;
    
    import com.getcapacitor.BridgeActivity;
    import com.getcapacitor.Plugin;
    
    import java.util.ArrayList;
    import com.getcapacitor.plugin.http.Http; //added
    
    public class MainActivity extends BridgeActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Initializes the Bridge
        this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
          // Additional plugins you've installed go here
          // Ex: add(TotallyAwesomePlugin.class);
          add(Http.class); //added
        }});
      }
    }
    

    2- 在 Android Studio 中同步 gradle 文件:

    File --> Sync Project with Gradle Files
    

    我根据自己的需要编写了http请求,现在可以完美运行了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-16
      • 2016-09-04
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      相关资源
      最近更新 更多