Axios get in url 有效,但第二个参数作为对象却无效 [英] Axios get in url works but with second parameter as object it doesn't

查看:28
本文介绍了Axios get in url 有效,但第二个参数作为对象却无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 GET 请求作为第二个参数发送,但是当它作为 url 发送时它不起作用.

I'm trying to send GET request as second parameter but it doesn't work while it does as url.

这有效,$_GET['naam'] 返回测试:

This works, $_GET['naam'] returns test:

export function saveScore(naam, score) {
  return function (dispatch) { 
    axios.get('http://****.nl/****/gebruikerOpslaan.php?naam=test')
      .then((response) => {
        dispatch({type: "SAVE_SCORE_SUCCESS", payload: response.data})
      })
      .catch((err) => {
        dispatch({type: "SAVE_SCORE_FAILURE", payload: err})
      })
  }
};

但是当我尝试这个时,$_GET 中什么都没有:

But when I try this, there is nothing in $_GET at all:

export function saveScore(naam, score) {
  return function (dispatch) { 
    axios.get('http://****.nl/****/gebruikerOpslaan.php',
    {
        password: 'pass',
        naam: naam,
        score: score
    })
      .then((response) => {
        dispatch({type: "SAVE_SCORE_SUCCESS", payload: response.data})
      })
      .catch((err) => {
        dispatch({type: "SAVE_SCORE_FAILURE", payload: err})
      })
  }
};

为什么我不能这样做?在文档中它清楚地表明这是可能的.使用 $_POST 它也不起作用.

Why can't I do that? In the docs it clearly says it's possible. With $_POST it doesn't work either.

推荐答案

axios.get 接受请求配置作为第二个参数(不是查询字符串参数).

axios.get accepts a request config as the second parameter (not query string params).

您可以使用 params 配置选项来设置查询字符串参数,如下所示:

You can use the params config option to set query string params as follows:

axios.get('/api', {
  params: {
    foo: 'bar'
  }
});

这篇关于Axios get in url 有效,但第二个参数作为对象却无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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