我有一个 Spring REST API,我已经在 Postman 上测试过它,它返回完全有效的 JSON。但是,当我在前端 React 代码上调用相同的 API 时,它会返回 HTML。这是函数,我用来调用 API,
export function run(engine, mediaId, owner, target, access){
let url = engine + "/" + mediaId + "?id=" + owner + "&targetId=" + target + "&access=" + access;
return fetch(full_url, { credentials: "include",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
}})
.then((response) => {
return response.json();})
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
}
Run Code Online (Sandbox Code Playgroud)
我在此调用中遇到语法错误Unexpected token < Thus when I check using response.text()我可以看到返回的数据是 HTML 而不是 JSON。我需要在前端代码中更改哪些 API 才能返回 JSON。
小智 14
标题不正确。JSON 响应的有效标头:
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28695 次 |
| 最近记录: |