response.text() 中的额外引号

Sam*_*Sam 7 javascript redux

在我的 React/Redux 应用程序中,我调用后端 API,该 API 返回文本响应。然后我使用以下行来检索文本。我看到的问题是我的代码似乎在我收到的文本周围放置了两组引号。

所以,我的 API 返回Hello World!但在下面的代码中它变成了“Hello World!”

我的 API 实际上返回一个字符串,因此文本周围总会有一组引号,例如“Hello World!” 这完全没问题。我只是不明白为什么我会收到两组报价。

知道为什么吗?

export const callApi = (request) => {
    return (dispatch) => fetch('/api/getsometext', fetchOptionsPost(request))
        .then((response) => {
            if(!response.ok) {
                // Request failed
                dispatch(setBadRequest(true))
            } else {
                const myText = response.text() // This is where I'm getting double quotes
                .then((myText) => dispatch(setMyText(myText)))
            }
        })
}
Run Code Online (Sandbox Code Playgroud)

Dej*_*jan 9

简单地引用 @Kaiido 的提示作为答案,这样它就不会在评论中迷失:

您的服务器发送带有这些引号的数据,可能是因为它认为应该将其作为 JSON 发送。因此,要么重新配置您的 API,使其不会尝试将其作为 JSON 发送,要么简单地使用 Response.JSON() 以便您的客户端正确解析它。