问题可能是axios的默认请求Content-Type
设置为application/json
,而不是application/x-www-form-urlencoded
,需要手动设置Content-Type
。可以使用qs
库将数据序列化为url-encoded格式,并将其设置为axios请求的请求体。具体代码示例如下:
import axios from "axios";
import qs from "qs";
axios.defaults.headers.post["Content-Type"] =
"application/x-www-form-urlencoded";
const params = { key1: "value1", key2: "value2" };
axios.get("/api", { params: qs.stringify(params) }).then((response) => {
console.log(response.data);
});