获取请求 response.json() returns 未定义
Fetch Request response.json() returns undefined
我想获取对我的 spring 启动服务器的获取请求,我得到了我的 json,但是当我想 return 它时,出现了一个未定义的错误.我是 Javascript 的新手,所以答案可能很明显,但我找不到!
抱歉英语不好,谢谢!
代码:
function httpGet(theUrl)
{
fetch(theUrl,
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then (response => response.json())
.then(response => {
console.log(response); // Logs the json array
return response; // Returns undefined
});
}
使用异步编辑,仍然无效:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
async function httpGet(theUrl)
{
const response = await fetch(theUrl,
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
});
const jsonResponse = await response.json()
.then(data => {
return data;
});
}
这是我的反应组件功能:
function Admin(){
const data = httpGet('https://jsonplaceholder.typicode.com/users'); // Not Working
console.log(data);
return(
<div>
<h1> Admin Page</h1>
</div>
)
}
function httpGet(theUrl) {
return fetch(theUrl, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then(response => response.json());
}
(async() => {
const response = await httpGet('https://jsonplaceholder.typicode.com/users');
console.log(response);
})();
您可以使用 async
和 await
来执行此操作,这样可以轻松编写和理解代码。
这是您可以使用的示例。
const httpGet = async (theUrl) => {
const response = await fetch(theUrl, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
return response.json();
};
// calling the method
(async () => {
const data = await httpGet("https://jsonplaceholder.typicode.com/posts/1")
console.log("response data is: ", data)
})()
我想获取对我的 spring 启动服务器的获取请求,我得到了我的 json,但是当我想 return 它时,出现了一个未定义的错误.我是 Javascript 的新手,所以答案可能很明显,但我找不到!
抱歉英语不好,谢谢!
代码:
function httpGet(theUrl)
{
fetch(theUrl,
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then (response => response.json())
.then(response => {
console.log(response); // Logs the json array
return response; // Returns undefined
});
}
使用异步编辑,仍然无效: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
async function httpGet(theUrl)
{
const response = await fetch(theUrl,
{
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
});
const jsonResponse = await response.json()
.then(data => {
return data;
});
}
这是我的反应组件功能:
function Admin(){
const data = httpGet('https://jsonplaceholder.typicode.com/users'); // Not Working
console.log(data);
return(
<div>
<h1> Admin Page</h1>
</div>
)
}
function httpGet(theUrl) {
return fetch(theUrl, {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then(response => response.json());
}
(async() => {
const response = await httpGet('https://jsonplaceholder.typicode.com/users');
console.log(response);
})();
您可以使用 async
和 await
来执行此操作,这样可以轻松编写和理解代码。
这是您可以使用的示例。
const httpGet = async (theUrl) => {
const response = await fetch(theUrl, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
}
});
return response.json();
};
// calling the method
(async () => {
const data = await httpGet("https://jsonplaceholder.typicode.com/posts/1")
console.log("response data is: ", data)
})()