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

查看:1577
本文介绍了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. Domain - http://yourdomain.com
  2. Path - /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天全站免登陆