【问题标题】:Get plain text from REST response从 REST 响应中获取纯文本
【发布时间】:2018-12-02 00:55:02
【问题描述】:

我使用 fetch 从我的 REST 服务中获取数据。响应到达后,我想获取错误消息(如果有),并将其显示给用户(用户主要是我)。

fetch(address, attributes).then(response => {
    if (!response.ok) {
        if (response.status === 404) {
            return {
                text: "Server not found!",
                status: "danger"
            };
        }
        return {
            text: response.text(),
            status: "danger"
        };
    }
    return {
        text: "Success!",
        status: "success"
    };
}

response.text() 部分是重要的部分:我的后端发送一个带有字符串作为实体的 javax.rs 响应,例如“错误”。在这里,我想阅读它,但是 response.text() 仅返回一个 Promise 对象,该对象在解决时仅返回更多 Promise 对象。

我也尝试使用{"msg":"[error]"},然后在此处将其解析为reponse.json().msg,但这也不起作用。

【问题讨论】:

    标签: reactjs rest promise fetch


    【解决方案1】:

    // Success test case
    fetch("https://jsonplaceholder.typicode.com/posts/1").then(response => {
        if (!response.ok) {
            if (response.status === 404) {
                return {
                    text: "Server not found!",
                    status: "danger"
                };
            }
    
            return response.text().then(text => {
                return {
                    text: text,
                    status: "danger"
                };
            })
        }
        return {
            text: "Success!",
            status: "success"
        };
    }).then(resp => {
        console.log("result:", resp);
    })
    
    // Failure test case 404
    fetch("https://jsonplaceholder.typicode.com/posts/Not_Exist").then(response => {
        if (!response.ok) {
            if (response.status === 404) {
                return {
                    text: "Server not found!",
                    status: "danger"
                };
            }
    
            return response.text().then(text => {
                return {
                    text: text,
                    status: "danger"
                };
            })
        }
        return {
            text: "Success!",
            status: "success"
        };
    }).then(resp => {
        console.log("result:", resp);
    })
    
    // For testing Errors other then 404, i changed 404 error in the code because i couldn't get other HTTP Errors from placeholder api
    
    fetch("https://jsonplaceholder.typicode.com/posts/Not_Exist").then(response => {
        if (!response.ok) {
            if (response.status === 999) {
                return {
                    text: "Server not found!",
                    status: "danger"
                };
            }
    
            return response.text().then(text => {
                return {
                    text: text,
                    status: "danger"
                };
            })
        }
        return {
            text: "Success!",
            status: "success"
        };
    }).then(resp => {
        console.log("result:", resp);
    })

    是的text() 函数返回promise object。因此,一种解决方案可能是以这种方式使用它:

    fetch(address, attributes).then(response => {
        if (!response.ok) {
            if (response.status === 404) {
                return {
                    text: "Server not found!",
                    status: "danger"
                };
            }
    
            return response.text().then(text => {
                return {
                    text: text,
                    status: "danger"
                };
            })
        }
        return {
            text: "Success!",
            status: "success"
        };
    })
    

    json()函数也可以这样使用:

    response.json().then(json => {
        return {
            text: json.msg,
            status: "..."
        };
    })
    

    编码愉快!

    【讨论】:

    • 遗憾的是,这对我来说不太适用;似乎 then() 仅将数组返回到下一个 then() 而不是使用函数的返回。因此,我总是得到“成功!”即使操作在内部失败。
    • @Hathics 哎呀我忘了添加return。我编辑了答案,现在查看测试用例。
    • 快乐编码 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多