reactjs使用axios发出https(不是http)请求

时间:2018-12-02 02:54:28

标签: reactjs https axios

我正在尝试使用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请求吗?

1 个答案:

答案 0 :(得分:2)

所有网址都有两个部分

  1. 域-http://yourdomain.com
  2. 路径-/path-to-your-endpoint

1。使用默认域

axios中,如果仅指定path,则默认情况下它将使用地址栏中的域。

例如,下面的代码将调用您地址栏中的任何域,并将此路径附加到该域。如果域为http,则您的api请求将为http调用;如果域为https,则api请求将为https调用。通常localhosthttp,您将在http中进行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)
});