reactjs 使用 axios 发出 https(不是 http)请求 [英] reactjs make https (not http) requests with axios

查看:41
本文介绍了reactjs 使用 axios 发出 https(不是 http)请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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

  1. 域 - http://yourdomain.com
  2. 路径 - /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 调用.通常localhosthttp,您将在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆