reactjs make https (not http) requests with axios

守給你的承諾、 提交于 2019-12-12 19:35:56

问题


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)
})

Can anyone help me convert this to an https request?


回答1:


All URLs have two parts

  1. Domain - http://yourdomain.com
  2. Path - /path-to-your-endpoint

1. Use default domain

In axios, if you specify just the path, it will use the domain in the address bar by default.

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. Specify full URL with domain

On the other hand, you can pass full URL to axios request and you will be making https calls by default.

axios.post('https://yourdomain.com/api/login/authentication', {

2. Use axios baseURL option

You can also set baseURL in axios

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)
});


来源:https://stackoverflow.com/questions/53576923/reactjs-make-https-not-http-requests-with-axios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!