如何检查 API 的 response JSON 是否为空或有错误?
How to check whether response JSON of an API is empty or has an error?
我是reactjs的新手,但遇到了一个问题。我正在调用 PUT 类型的更新 API。我使用 fetch 函数在 reactjs 中调用 API 并检查 API 的响应。如果 Response 是 200 OK,那么我 return response.json()
然后检查 json 对象是否有错误。如果它有错误,那么我打印错误,否则我更新它。
但是当响应中没有错误时,我会在 return response.json()
语句中收到语法错误,如果响应中确实存在错误,则不会显示语法错误。那么有没有一种方法可以检查响应是否为空,这样我就可以 return response.json()
.
我试过将条件设置为 if(response.json() != '')
但它在 response.json()
语句中显示错误。
fetch( API + name , {
method: 'PUT',
headers: {
'Accept' : 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
},
body: JSON.stringify({
name: name,
description: updateDesc
}),
}).then(function(response) {
if(response.status == '200'){
flag=true;
return response.json();
}
else {
flag=false
}
})
.then(json => {
if(flag)
{
if(json.Error != "")
{
that.createNotification('error','update');
}
else {
this.createNotification('success','update');
}
}
});
未处理的拒绝(SyntaxError):JSON 输入意外结束
此 imo 存在多个问题:
应重构回调以避免使用标志变量。提供给 then
、catch
和 finally
等处理程序的函数中的代码是异步执行的。因此,您无法确定/(不应假设)何时分配此值以及当时您的上下文处于何种状态。
.then(json => {
如果出现错误,这实际上将使用 fetch
又名 response
返回的承诺,而不是 response.json()
返回的承诺(目前return response.json()
只在成功的情况下执行)
请注意,发生这种情况(目前在错误情况下有效)是因为您可以链接承诺。您可以找到有关此 here
的更多信息和示例
我会像这样重构获取承诺的处理:
- 您可以缩短以下示例并避免分配承诺,但这会使示例更易读
- More information about the response object
const fetchPromise = fetch(<your params>);
fetchPromise.then(response => {
if (response.ok()){
//Your request was successful
const jsonPromise = response.json();
jsonPromise.then(data => {
console.log("Successful request, parsed json body", data);
}).catch(error => {
//error handling for json parsing errors (empty body etc.)
console.log("Successful request, Could not parse body as json", error);
})
} else {
//Your request was not successful
/*
You can check the body of the response here anyways. Maybe your api does return a json error?
*/
}
}).catch(error => {
//error handling for fetch errors
}))
我是reactjs的新手,但遇到了一个问题。我正在调用 PUT 类型的更新 API。我使用 fetch 函数在 reactjs 中调用 API 并检查 API 的响应。如果 Response 是 200 OK,那么我 return response.json()
然后检查 json 对象是否有错误。如果它有错误,那么我打印错误,否则我更新它。
但是当响应中没有错误时,我会在 return response.json()
语句中收到语法错误,如果响应中确实存在错误,则不会显示语法错误。那么有没有一种方法可以检查响应是否为空,这样我就可以 return response.json()
.
我试过将条件设置为 if(response.json() != '')
但它在 response.json()
语句中显示错误。
fetch( API + name , {
method: 'PUT',
headers: {
'Accept' : 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + localStorage.getItem('access_token')
},
body: JSON.stringify({
name: name,
description: updateDesc
}),
}).then(function(response) {
if(response.status == '200'){
flag=true;
return response.json();
}
else {
flag=false
}
})
.then(json => {
if(flag)
{
if(json.Error != "")
{
that.createNotification('error','update');
}
else {
this.createNotification('success','update');
}
}
});
未处理的拒绝(SyntaxError):JSON 输入意外结束
此 imo 存在多个问题:
应重构回调以避免使用标志变量。提供给
then
、catch
和finally
等处理程序的函数中的代码是异步执行的。因此,您无法确定/(不应假设)何时分配此值以及当时您的上下文处于何种状态。.then(json => {
如果出现错误,这实际上将使用fetch
又名response
返回的承诺,而不是response.json()
返回的承诺(目前return response.json()
只在成功的情况下执行)请注意,发生这种情况(目前在错误情况下有效)是因为您可以链接承诺。您可以找到有关此 here
的更多信息和示例
我会像这样重构获取承诺的处理:
- 您可以缩短以下示例并避免分配承诺,但这会使示例更易读
- More information about the response object
const fetchPromise = fetch(<your params>);
fetchPromise.then(response => {
if (response.ok()){
//Your request was successful
const jsonPromise = response.json();
jsonPromise.then(data => {
console.log("Successful request, parsed json body", data);
}).catch(error => {
//error handling for json parsing errors (empty body etc.)
console.log("Successful request, Could not parse body as json", error);
})
} else {
//Your request was not successful
/*
You can check the body of the response here anyways. Maybe your api does return a json error?
*/
}
}).catch(error => {
//error handling for fetch errors
}))