reactjs使用axios发出https(不是http)请求 [英] reactjs make https (not http) requests with axios
问题描述
我正在尝试使用axios向服务器发出https请求.有关axios的大多数教程都指定如何发出http请求. 每当用户登录时,我都会发出请求.这是我当前的请求:
I'm trying to make https requests to the server using axios. Most of the tutorials regarding axios specify how to make http requests. I make the requests whenever users login. Here is my current request:
axios.post('/api/login/authentication', {
email: email,
password: password
})
.then(response => {
this.props.history.push('/MainPage')
})
.catch(error => {
console.log(error)
})
有人可以帮我将其转换为https请求吗?
Can anyone help me convert this to an https request?
推荐答案
所有URL都有两个部分
All URLs have two parts
- 域-
http://yourdomain.com
- 路径-
/path-to-your-endpoint
- Domain -
http://yourdomain.com
- Path -
/path-to-your-endpoint
1.使用默认域
在axios
中,如果仅指定path
,则默认情况下它将使用地址栏中的域.
1. Use default domain
In axios
, if you specify just the path
, it will use the domain in the address bar by default.
例如,下面的代码将调用您地址栏中的任何域,并将此路径附加到该域.如果域是http
,则您的api请求将是http
调用;如果域是https
,则api请求将是https
调用.通常localhost
是http
,您将在localhost
中进行http
调用.
For example, the code below will make a call to whatever domain is in your address bar and append this path to it. If the domain is http
, your api request will be a http
call and if the domain is https
, the api request will be a https
call. Usually localhost
is http
and you will be making http
calls in localhost
.
axios.post('/api/login/authentication', {
2.用域指定完整的URL
另一方面,您可以将完整的URL传递给axios请求,默认情况下将进行https
调用.
axios.post('https://yourdomain.com/api/login/authentication', {
2.使用axios 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)
});
这篇关于reactjs使用axios发出https(不是http)请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!