简体   繁体   English

错误:在 React Native 中使用 axios 的网络错误,仅在 iOS 和 Android 中出现

[英]Error: Network Error using axios in React Native, issue in only in iOS and Android

Hi to all i've a problem to call apis of my .net-core server from iOS and Android simulators, while from browser it works.大家好,我在从 iOS 和 Android 模拟器调用我的 .net-core 服务器的 api 时遇到问题,而从浏览器它可以工作。 In my Backend CORS are enabled:在我的后端 CORS 启用:

public void ConfigureServices(IServiceCollection services){
            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

            services.AddMvc();

            services.AddControllers();
        }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
    if (env.IsDevelopment()){
         app.UseDeveloperExceptionPage();
    }
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseCors("MyPolicy");
    app.UseEndpoints(endpoints =>{
       endpoints.MapControllers();
    });
}

The axios GET service: axios GET 服务:

export function axiosGet():Promise<any>{

  
  const url = "https://localhost:5004/api/Aliments/";
  
  return axios.get(url)
      .then(response => {

          return response.data;
      })
      .catch(error => {
          console.log(error);
      });
};

In your opinion why when i call APIs from IOS/Android simulators return me this error?在您看来,为什么当我从 IOS/Android 模拟器调用 API 时会返回此错误?

Error: Network Error
    at createError (createError.js:16)
    at EventTarget.handleError (xhr.js:84)
    at EventTarget.dispatchEvent (event-target-shim.js:818)
    at EventTarget.setReadyState (XMLHttpRequest.js:600)
    at EventTarget.__didCompleteResponse (XMLHttpRequest.js:395)
    at XMLHttpRequest.js:508
    at RCTDeviceEventEmitter.emit (EventEmitter.js:189)
    at MessageQueue.__callFunction (MessageQueue.js:416)
    at MessageQueue.js:109
    at MessageQueue.__guard (MessageQueue.js:364)

EDIT 1 i've tried to change the host ip in 192.168.1.104(is the lan ip where is hosted my server)编辑 1 我试图在 192.168.1.104 中更改主机 ip(是我的服务器托管的局域网 ip)

this is my launchSettings.json这是我的launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:59803",
      "sslPort": 44303
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "MyApp",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyApp": {
      "commandName": "Project",
      "launchBrowser": false,
      "launchUrl": "MyApp",
      "applicationUrl": "https://192.168.1.104:5004;",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Simulators ip is different from your host machine's ip.模拟器 ip 与主机的 ip 不同。 Although they are in the same LAN.虽然他们在同一个局域网。 You can change the ip in this url.您可以在此 url 中更改 ip。

const url = "https://[host ip]:5004/api/Aliments/";

The ip can be queried with: ipconfig in cmd. ip 可以通过以下方式查询: ipconfig in cmd。

You are using https on localhost (pretty sure an auto signed certificate), on a non standard https port.您在非标准 https 端口上使用 https 本地主机(很确定自动签名证书)。 In development (unless special cases) you can use an http protocol.在开发中(除非特殊情况),您可以使用 http 协议。 Try to move from https to http, it should woks fine.尝试从 https 移动到 http,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM