【问题标题】:Send axios fetch to domain name, and resending it to API将 axios fetch 发送到域名,并重新发送到 API
【发布时间】:2022-01-20 23:42:07
【问题描述】:

我想解决 react native 和 iphone 的问题。我在 Gcloud 上有 API,比如说 ip 34.xx.xx.xx,还有一个域名 myapp.com。在域名 myapp.com 上,我有 SSL 证书和 https,在 ip 地址上,我有带有 https 的 nginx 服务器,但没有证书。我该怎么做才能使 axios 获取对我的域名的请求,而不会出现 CORS 错误等。

我解决 CORS 问题的一种方法是 nginx 设置:

# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}

使用此设置 CORS 错误消失,API 停止发送数据,在 Postman 中我收到此错误: Postman error screenshot

这是获取请求:

axios.post(`${Ip}login`,
            {
                email: email,
                password: password
            },
            {headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',            
            },
            })
          .then(function (response) {
            const stringToken = String(response.data.access_token)
            setToken(stringToken)
            sighIn(stringToken)
            setLoading(false)
          })
          .catch(function (error) {
            console.log("login section")

【问题讨论】:

  • 请嵌入你得到的错误以便能够帮助你

标签: react-native axios fetch-api fastapi


【解决方案1】:

你可以使用axios的create函数

const instance = axios.create({
  baseURL: 'https://myapp.com',
  headers: { 'any': 'any' },
  timeout: 1000,
});

instance.get("/any")

【讨论】:

  • @МихаилЖурин 如果我的回答有用,请点击它左侧的点赞按钮 (▲)。如果它回答了您的问题,请单击复选标记 (✓) 接受它。这样其他人就知道你已经(充分地)得到了帮助。另请参阅 stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2019-03-09
  • 2011-08-02
  • 2021-08-08
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多