Axios在React Native/NodeJS环境中默认不执行SSL验证,因此需要手动启用。可以通过设置“httpsAgent”属性来自定义代理选项。以下代码展示了如何在React Native中启用SSL验证:
import axios from 'axios';
import https from 'https';
let agent = new https.Agent({
rejectUnauthorized: false
});
axios.get('https://example.com', { httpsAgent: agent })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
或者,您也可以在Node.js中使用以下代码:
const axios = require('axios');
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false
});
axios.get('https://example.com', { httpsAgent: agent })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
这里我们使用“rejectUnauthorized”选项设置为“false”以解决SSL验证问题。但是,在生产环境中,为了确保安全性,您应该设置为“true”。