我正在尝试使用axios向服务器发出https请求。有关axios的大多数教程都指定如何发出http请求。每当用户登录时,我都会发出请求。这是我目前的要求:
axios.post('/api/login/authentication', { email: email, password: password }) .then(response => { this.props.history.push('/MainPage') }) .catch(error => { console.log(error) })
谁能帮我将其转换为https请求?
所有网址都有两个部分
http://yourdomain.com
/path-to-your-endpoint
在中axios,如果仅指定path,则默认情况下它将使用地址栏中的域。
axios
path
例如,下面的代码将调用您地址栏中的任何域,并将此路径附加到该域。如果域是http,则您的api请求将是一个http调用,如果域是https,则api请求将是一个https调用。通常localhost是http,您将http拨入localhost。
http
https
localhost
axios.post('/api/login/authentication', {
另一方面,您可以将完整的URL传递给axios请求,并且https默认情况下将进行呼叫。
axios.post('https://yourdomain.com/api/login/authentication', {
您也可以baseURL在axios中设置
baseURL
axios({ method: 'post', baseURL: 'https://yourdomain.com/api/', url: '/login/authentication', data: { email: email, password: password } }).then(response => { this.props.history.push('/MainPage') }) .catch(error => { console.log(error) });