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

我正在尝试使用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. 域- 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选项

您也可以baseURL在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)

});

以上是 reactjs使用axios发出https(不是http)请求 的全部内容, 来源链接: utcz.com/qa/435789.html

回到顶部