通常出现这个错误的原因是由于接口返回的数据格式与代码中定义的不一致。可以使用console.log()查看返回的数据格式并进行检查,也可以将返回的数据打印出来以便更直观的观察。
以下示例代码中,将打印输出(console.log())添加到请求中,确定返回的数据格式并将其设置为正确的格式。
axios.post('/api/login', { username: 'johnDoe', password: 'pwd123' }) .then(function (response) { console.log(response.data); // 添加打印输出 }) .catch(function (error) { console.log(error); });