React-使用axios的GET和POST请求

时间:2019-10-25 16:51:46

标签: javascript reactjs xmlhttprequest axios

在我的应用程序中,我需要std::numeric_limits<int>::max();一些数据(为此我提供了本地身份验证令牌)。

但是,在同一事件中,我还需要GET第二个令牌,以供外部端点api调用的几个端点使用。

如何使用下面的工作代码POST POST继续使用此 second 令牌?

我应该扩展授权承载还是仅将axios Spotify令牌作为字符串数据? 怎么这样?

我的代码:

POST

2 个答案:

答案 0 :(得分:0)

对于应用到您的代码的异步等待,看起来像这样。

async getData(event) {
  const {token} = this.props.spotifyToken

  let getRes = await axios.get(`${process.env.URL}/endpoint` {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${window.localStorage.authToken}`
    }
  }

  let postRes = await axios.post(`${process.env.URL}/endpoint` {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${window.localStorage.authToken}`
    }
  }

  console.log(getRes.data.data);
  console.log(postRes.data.data);
};

答案 1 :(得分:0)

在这种特定情况下,需要令牌来在后端获取数据,我发现在url处传递令牌更合适,就像这样:

myButLast

和:

@endpoint.route('/endpoint/<select>/<user_id>/<token>', methods=['GET'])
def endpoint(name, user_id, token):
# business logic

否则,对于POST和GET,后端代码将运行两次,对于我而言,这是不希望的。

相关问题