[英]React Native returns network error when calling asp.net api
I am trying to call my asp.net api from react native but it always returns a.network error.我正在尝试从 React Native 调用我的 asp.net api 但它总是返回 a.network 错误。 I have configured my api cors policy to accept all origins, all methods and all headers.
我已将我的 api cors 策略配置为接受所有来源、所有方法和所有标头。 I am running the react native app on my ios phone using expo, I configured the url to https as I have read ios blocks http requests by default.
我正在使用 expo 在我的 ios 手机上运行 React Native 应用程序,我将 url 配置为 https,因为我默认读取了 ios 块 http 请求。 I tried using localhost:{port} as my url and my server's ip address and nothing's working.
我尝试使用 localhost:{port} 作为我的 url 和我服务器的 ip 地址,但没有任何效果。 I have been stuck in this issue for 3 days and would appreciate any help.
我已经在这个问题上停留了 3 天,非常感谢任何帮助。
my c# code:我的 c# 代码:
builder.Services.AddCors(opt =>
{
opt.AddPolicy("CorsPolicy", policy =>
{
policy.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
my get request is a very simple get request for testing purposes我的获取请求是一个非常简单的获取请求,用于测试目的
[AllowAnonymous]
[HttpGet("Get")]
public async Task<ActionResult> Get()
{
return Ok("test");
}
my react native code to call the get method:我的本机代码调用 get 方法:
let getEmployees = () => {
fetch("https://localhost:7287/get")
.then((res) => {
console.log(res.status);
console.log(res.headers);
return res.json();
})
.then(
(result) => {
console.log(result);
},
(error) => {
console.log(error);
}
);
};
here in my react native code, i'm using localhost but I tried to use my server's ip address as well but still, I get.network request failed.在我的 React 本机代码中,我使用的是 localhost,但我也尝试使用服务器的 ip 地址,但我仍然 get.network 请求失败。
here's the error i'm getting from react native这是我从本机反应中得到的错误
Network request failed
at node_modules\whatwg-fetch\dist\fetch.umd.js:541:17 in setTimeout$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:214:12 in _allocateCallback$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:112:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:357:16 in callTimers
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:417:4 in __callFunction
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:114:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:368:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:113:4 in callFunctionReturnFlushedQueue
One final thing to note is that all of my api endpoints work perfectly on the browser, postman and swagger. I even tried them on a simple react.js project and they work.最后要注意的一件事是,我的所有 api 端点 postman 和 swagger 在浏览器上都可以正常工作。我什至在一个简单的 react.js 项目上尝试过它们,它们都可以工作。 Issue only occurs on the react native project.
问题仅发生在 React Native 项目上。 Any help would be massively appreciated.
任何帮助将不胜感激。
Can you try with ngrok.io?你能试试 ngrok.io 吗? It'll generate a url which you can use in your application.
它将生成一个 url,您可以在您的应用程序中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.