前端获取后端返回值显示undefined问题的修复方案
问题解决:前端回调函数无法获取后端返回属性值
问题描述
拥有名为wsSentinelhubEngineProcessor的Web服务,需在前端展示后端返回的processAPIResCode、processAPIContents、processAPIPathToTIFF数据。当前调用fetchNDVIsTIFF的回调函数时,所有参数均显示undefined,但then方法中可正常获取后端返回内容。
问题根源
Web服务代码中使用response.text()解析后端返回的JSON数据,导致返回结果为字符串而非对象,无法直接访问对象属性。
修复方案
将Web服务中的response.text()替换为response.json(),修改后代码如下:
await fetch(ws, this.#requestOptions) .then(response => response.json()) .then(response => { resolve(response); onResult(response.processAPIResCode, response.processAPIContents, response.processAPIPathToTIFF); })
修改后,回调函数即可正确获取后端返回的属性值。
内容的提问来源于stack exchange,提问作者Amr
相关产品推荐
相关产品推荐

