我正在尝试使用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请求吗?
答案 0 :(得分:2)
所有网址都有两个部分
http://yourdomain.com
/path-to-your-endpoint
在axios
中,如果仅指定path
,则默认情况下它将使用地址栏中的域。
例如,下面的代码将调用您地址栏中的任何域,并将此路径附加到该域。如果域为http
,则您的api请求将为http
调用;如果域为https
,则api请求将为https
调用。通常localhost
是http
,您将在http
中进行localhost
呼叫。
axios.post('/api/login/authentication', {
另一方面,您可以将完整的URL传递给axios请求,并且默认情况下将进行https
个呼叫。
axios.post('https://yourdomain.com/api/login/authentication', {
您还可以在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)
});