【问题标题】:Axios - send get request with url + variableAxios - 使用 url + 变量发送获取请求
【发布时间】:2021-08-20 03:03:54
【问题描述】:

这一定是个愚蠢的问题,但我才刚刚开始,希望能得到任何帮助!

所以我有这段代码来获取查询参数:

app.get('/', (req, res) => {
var code = req.query.code;
console.log(code);

当我访问 http://localhost:3000/?code=123 时,我在控制台中得到了代码值,所以它工作正常。

现在,我需要发送一个 GET 请求并添加 var 代码的值,这就是我卡住的地方。 比方说,我应该向 'http://testtesttest123.com/' + var code + 'hi' 发送一个 GET 请求。

我该怎么做?

我已经尝试过这种方法和其他一些方法,但没有任何效果:

 axios.get('http://testtesttest123.com/?&code=', {params: {code}}, '&hi')
.then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
});

提前谢谢你!

【问题讨论】:

标签: node.js axios


【解决方案1】:

使用params 配置通过查询参数发送。要支持您的空 hi 参数,您可以将其包含在 URL 字符串中

axios.get("http://testtesttest123.com/?hi", {
  params: { code }
})

对于code 值为123,这将执行一个GET 请求

http://testtesttest123.com/?hi&code=123

它还将确保 code 值可以安全地在 URL 中使用

【讨论】:

    【解决方案2】:

    axios.get 调用应如下所示。

    axios.get('http://testtesttest123.com/?code=' + code + '&hi')
    

    使用code = 123,这将调用URL http://testtesttest123.com/?code=123&hi

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 1970-01-01
      • 2018-10-22
      • 2018-07-11
      • 2011-11-08
      • 2021-09-24
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多