无法发送request.data并获得api响应?

时间:2019-11-12 11:25:01

标签: reactjs axios

使用Axios请求从API提取数据

export const getReports = async (url, obj) => {
    return new Promise(
        async (resolve, reject) => {
            try {
                const data = await axios.request(
                    {
                        method: 'get',
                        url: BASE_URL+url,
                        headers: {

                            'Authorization':  `Bearer ${await getAccessToken()}`
                        },
                        data: {date_to:'2019-11-05',date_from:'2019-11-05',c_name:'1'}
                    }
                );
                resolve(data);
            } catch (e) {
                reject(e)
            }
        }
    )
};

出错,无法在后端接收request.data ...

从事邮递员工作

体内

 -date_to:'2019-11-05'  -date_from:'2019-11-05'  -c_name:“ 1”

1 个答案:

答案 0 :(得分:0)

如果您具有GET axios请求类型,则不使用数据字段。

(来自文档)

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  },

数据字段用于POST,PUT,PATCH等。您可以尝试使用查询参数或更改请求的类型。

这里是关于文档中的查询参数

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  }

所以它将显示为url.com?ID=12345

然后您可以从后端解析它们。这也是HTTP标准,您不应尝试使用GET请求发送正文数据。