问题描述
我找到了(问题) 获取请求在 Android SDK 平台 23 (Android 6) 上成功结束,但在 Android SDK 平台 24 (Android 7) 上出现错误网络请求失败":
I have found (question) that fetch request ends successfuly on Android SDK Platform 23 (Android 6), but on Android SDK Platform 24 (Android 7) I have error 'Network request failed':
try {
let res = await fetch('https://pricesea.ru/api/auth/login', {
method: 'POST',
credentials: 'include',
headers: new Headers({'Accept': 'application/json', 'Content-type': 'application/x-www-form-urlencoded'}),
body: `username=${this.state.username}&password=${this.state.password}`
});
if (res.ok) {
...
}
} catch (e) {
console.log(e);
}
错误:
TypeError: Network request failed
at XMLHttpRequest.xhr.onerror (fetch.js:441)
at XMLHttpRequest.dispatchEvent (event-target.js:172)
at XMLHttpRequest.setReadyState (XMLHttpRequest.js:546)
at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:381)
at XMLHttpRequest.js:485
at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
at MessageQueue.__callFunction (MessageQueue.js:260)
at MessageQueue.js:101
at MessageQueue.__guard (MessageQueue.js:228)
at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:100)
我正在使用 RN 0.46.3.
I am using RN 0.46.3.
这可能是什么原因?
我已经测试了从 22 到 26 的 API 版本,问题只出在 API 24(Android 7.0)上.在其他 API 版本上,请求成功结束.
I have tested API versions from 22 to 26, problem is only on API 24 (Android 7.0). On other API versions request ends successful.
编辑 2: Github RN 问题:https://github.com/facebook/react-native/issues/15100
EDIT 2: Github RN issue: https://github.com/facebook/react-native/issues/15100
推荐答案
这是 Android 7.0 上的一个错误,如 此处描述.Vicky Chijwani 提出的解决方法:
That's a bug on Android 7.0, as described here. Workaround proposed by Vicky Chijwani:
将您的服务器配置为使用椭圆曲线 prime256v1.为了例如,在 Nginx 1.10 中,您可以通过设置 ssl_ecdh_curve素数256v1;
Configure your server to use the elliptic curve prime256v1. For example, in Nginx 1.10 you do this by setting ssl_ecdh_curve prime256v1;
这篇关于React Native fetch() 在 Android 7 上抛出“网络请求失败",但在 Android 6 上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!