当API返回[object Object]
时,可以使用以下代码示例来解决问题:
fetch('api-url')
.then(response => response.json())
.then(data => {
console.log(data); // 检查返回的数据
// 处理返回的数据
})
.catch(error => {
console.log(error); // 处理错误
});
上述代码使用了fetch
函数来发送API请求,并使用.json()
方法将返回的响应转换为JSON格式。之后,可以在第二个.then()
回调函数中处理返回的数据。如果出现错误,可以在.catch()
回调函数中进行错误处理。
请注意,根据具体的API和数据结构,你可能需要根据返回的数据进行进一步的处理。