jim*_*jim 8 https reactjs 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)
})
Run Code Online (Sandbox Code Playgroud)
谁能帮我将其转换为 https 请求?
所有 URL 都有两部分
http://yourdomain.com
/path-to-your-endpoint
在 中axios
,如果您仅指定path
,则默认情况下它将使用地址栏中的域。
例如,下面的代码将调用地址栏中的任何域并将此路径附加到它。如果域是http
,则您的 api 请求将是http
调用;如果域是https
,则 api 请求将是https
调用。通常localhost
是http
,您将http
拨打localhost
.
axios.post('/api/login/authentication', {
Run Code Online (Sandbox Code Playgroud)
另一方面,您可以将完整 URL 传递给 axios 请求,https
默认情况下您将进行调用。
axios.post('https://yourdomain.com/api/login/authentication', {
Run Code Online (Sandbox Code Playgroud)
也可以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)
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18055 次 |
最近记录: |