React Native fetch()在Android 7上引发“网络请求失败",但在Android 6上运行良好
我发现了(问题 ),获取请求在Android SDK Platform 23(Android 6)上成功结束,但是在Android SDK Platform 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.
Github RN问题: https://github.com/facebook/react-native/issues/15100
EDIT 2: Github RN issue: https://github.com/facebook/react-native/issues/15100
That's a bug on Android 7.0, as described here. Workaround proposed by Vicky Chijwani:
配置服务器以使用椭圆曲线prime256v1.为了 例如,在Nginx 1.10中,您可以通过设置ssl_ecdh_curve来实现 prime256v1;
Configure your server to use the elliptic curve prime256v1. For example, in Nginx 1.10 you do this by setting ssl_ecdh_curve prime256v1;