【发布时间】:2021-10-07 15:28:22
【问题描述】:
我尝试使用 axios 和 fetch 从 API 端点获取数据,但结果只返回了一个空数组。它在邮递员中运行良好。我错过了什么?
使用 Axios
axios({
method: "get",
url: "/api/purchase/receivegoods/index?action=detail-grn",
params: {
goods_receive_id: 71,
},
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((response) => {
console.log(("response", response));
})
.catch((error) => {
console.log("error message: ", error.message);
});
使用 Fetch
fetch(
"https://devactive.com/api/purchase/receivegoods/index?action=detail-grn",
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
params: {
goods_receive_id: 71,
},
}
)
.then((response) => response.json())
.then((result) => {
console.log("Success:", result);
})
.catch((error) => {
console.error("Error:", error);
});
编辑: 请求采用 JSON 格式。它看起来像这样(这是现有 id 的示例:
{
"goods_receive_id" : 71
}
【问题讨论】:
-
您在 Postman 中提出的请求是什么样的?
-
json 格式。它只发送一个 json 格式的 id。
标签: axios httprequest httpresponse fetch-api