我在带有Conveyor扩展名的Visual Studio中创建了服务,以使其可以在本地网络中访问。我在Android设备上安装了证书,因此在浏览器中调用该证书时会出现绿色的挂锁,并且服务正常。 但是,当使用axios从React Native应用程序调用它时,我得到了网络错误。 那是我的代码:
const fetchData = async () => {
console.log('url', `${Settings.API_URL}/location/GetDataByMyIp`);
try {
const result = await axios(
`${Settings.API_URL}/location/GetDataByMyIp`,
);
console.log('result', result);
ipData = {
city: result.data.city,
longitude: result.data.longitude,
latitude: result.data.latitude,
};
} catch (e) {
console.log('error', e);
}
};
await fetchData();
在控制台中,我看到:
url https://192.168.1.14:45455/api/location/GetDataByMyIp
error Error: Network Error
at createError (path_to_app\node_modules\axios\lib\core\createError.js:16)
at EventTarget.handleError (path_to_app\node_modules\axios\lib\adapters\xhr.js:83)
at EventTarget.dispatchEvent (path_to_app\node_modules\event-target-shim\dist\event-target-shim.js:818)
at EventTarget.setReadyState (path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:575)
at EventTarget.__didCompleteResponse (path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:389)
at path_to_app\node_modules\react-native\Libraries\Network\XMLHttpRequest.js:502
at RCTDeviceEventEmitter.emit (path_to_app\node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189)
at MessageQueue.__callFunction (path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425)
at path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112
at MessageQueue.__guard (path_to_app\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373)
我试图将此行添加到AndroidManifest.xml应用程序标记中,但仍然出现错误
android:usesCleartextTraffic="true"
一些解决方案说要向axios添加第二个参数,但不适用于React Native。
new https.Agent({
rejectUnauthorized: false,
});
我试图从互联网上打电话给其他服务,然后错误消失了。
对此有解决方案吗?我什么都没有找到。我的最后一个想法是在Azure上托管服务,因此我将对SSL进行正确签名,但是我想它必须是在本地运行该服务的一种方式。
编辑:它通过http工作