【发布时间】:2017-03-28 22:00:38
【问题描述】:
我有一个在 Postman 上测试过的 Spring REST API,它返回完全有效的 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);
});
}
我在调用Unexpected token < Thus when I check using response.text() 时遇到语法错误,我可以看到返回的数据是 HTML 而不是 JSON。我需要在我的前端代码中更改哪些 API 才能返回 JSON。
【问题讨论】:
-
请尝试“Content-Type”:“text/plain”
-
你得到答案了吗?
标签: javascript json fetch-api