[英]Axios network error (Invalid imageTag) when querying a Flask resource from React Native iOS app
I have a Flask app set up with a few endpoints.我有一个 Flask 应用程序设置了几个端点。 These endpoints work fine when accessed from postman or a browser.
从 postman 或浏览器访问时,这些端点工作正常。
I also have a React Native app set up (using an Expo template), in which I try to query this resource from iOS:我还设置了一个 React Native 应用程序(使用 Expo 模板),我在其中尝试从 iOS 查询此资源:
const testApiCall = async (): Promise<string> => {
try {
const url = `${baseUrl}/users`;
const response = await axios.get(url);
console.log(response.data);
return response.data.toString();
} catch (error) {
console.log(error.request)
console.log(" a " + error.request?._response);
console.log(error);
}
}
baseUrl
is the full IP, not just localhost
. baseUrl
是完整的 IP,而不仅仅是localhost
。
The /users
endpoint returns json of the app's users. /users
端点返回应用程序用户的 json。 However, when calling this, I get the following output:但是,调用它时,我得到以下 output:
a Invalid imageTag: 192.168.1.191:80/users
[AxiosError: Network Error]
(excluding the error.request
since it's long and probably doesn't provide any additional context - will edit this post if it would be useful!) (不包括
error.request
,因为它很长并且可能不提供任何额外的上下文 - 如果它有用,将编辑这篇文章!)
I can't find any reason for the "invalid imageTag" error.我找不到“无效 imageTag”错误的任何原因。 Intermittently, the flask app will get logs including a 404 request for the favicon:
间歇性地,flask 应用程序将获取日志,包括对网站图标的 404 请求:
[06/May/2022 21:55:05] "GET /users HTTP/1.1" 200 -
[06/May/2022 21:55:05] "GET /favicon.ico HTTP/1.1" 404 -
[06/May/2022 21:56:15] "GET /users HTTP/1.1" 200 -
[06/May/2022 21:56:16] "GET /favicon.ico HTTP/1.1" 404 -
I even tried to make an endpoint in the flask app to return a generic.ico file when requesting /favicon.ico
but behavior didn't change.我什至尝试在 flask 应用程序中创建一个端点以在请求
/favicon.ico
时返回一个 generic.ico 文件,但行为没有改变。 Now, I stopped getting any kind of request on my flask app when using my React Native app.现在,当我使用 React Native 应用程序时,我的 flask 应用程序不再收到任何类型的请求。 I still get logs when accessing the endpoints from postman, though.
不过,从 postman 访问端点时,我仍然会收到日志。
I also used flask_cors
:我还使用
flask_cors
:
CORS(app, resources={r'/*': {'origins': '*'}})
This one has been a real head-scratcher for me and I'm having trouble finding any similar issues, so I'd really appreciate any help, Happy to provide additional context that could be useful.这对我来说真是一个令人头疼的问题,我很难找到任何类似的问题,所以我真的很感激任何帮助,很高兴提供可能有用的额外上下文。 too.
也。
A request to https://jsonplaceholder.typicode.com/todos/1 worked without an issue.对https://jsonplaceholder.typicode.com/todos/1的请求没有问题。 Tried with http:// and that worked too.
尝试使用 http:// 并且也有效。
Added this to app.json
but it didn't help:将此添加到
app.json
但没有帮助:
"ios": {
"supportsTablet": true,
"infoPlist": {
"NSAppTransportSecurity" : {
"NSAllowsArbitraryLoads" : true,
"NSAllowsLocalNetworking": true
}
}
},
Turns out my logic for getting the baseUrl
excluded the http://
.原来我的逻辑是让
baseUrl
排除http://
。 Prepending the baseUrl with 'http://'
solved the issue.在 baseUrl 前加上
'http://'
解决了这个问题。 It's always something obvious that I miss!我总是想念一些明显的东西!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.