javascript - response.text() 中的额外引号

标签 javascript redux

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

所以,我的 API 返回 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)))
            }
        })
}

最佳答案

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

Your server sends the data with these quotes, probably because it thinks it should send it as JSON. So either reconfigure your API so it doesn't try to send it as JSON, either simply use Response.JSON() so that your client-side parses it correctly.

关于javascript - response.text() 中的额外引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46147330/

相关文章:

Javascript switch 语句不起作用 - 三个太多了吗?

javascript - .not() 函数 jquery 隐藏其他元素

javascript - 获取参数截止值的函数

javascript - 如何访问 Redux reducer 中的状态?

reactjs - 如何从 Action Creator 访问 redux 存储?

javascript - 无法使用sequelize和mysql根据包含的模型属性过滤数据

javascript - 如何分割正则表达式空间和标点符号匹配,但将标点符号保留在结果数组中?

javascript - 如何在不不断重新安装的情况下将包装组件传递到 React Router

javascript - Action 必须是普通对象。使用自定义中间件进行异步操作 - React Native Redux

javascript - 如何提高React Redux性能?