【问题标题】:Draft.js Cannot convert undefined or null to object convertFromRawDraft.js 无法将 undefined 或 null 转换为对象 convertFromRaw
【发布时间】:2018-01-09 10:08:37
【问题描述】:

我将我的 html 代码以 json 格式保存到 sql 数据库中

const content = this.state.editorState.getCurrentContent();
const raw = JSON.stringify(convertToRaw(content))

原始是

{"entityMap":{},"blocks":[{"key":"4bk88","text":"ridee","type":"unstyled","depth":0,"inlineStyleRanges" :[],"entityRanges":[],"data":{}}]}

但是,当我在服务器中使用 XMLHTTPRequest 发送它时,它会将 req.body 更改为

post.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')

这就是它的保存方式

{"{\"entityMap\":{},\"blocks\":[{\"key\":\"Some Text\",\"text\":\"ridee\",\" type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":":[""]}

所以在我收到后它不会作为预期的对象返回

const raw = JSON.parse(req.response.htmlcode)
const contentState = convertFromRaw(raw);

{"entityMap":{},"blocks":[{"key":"Some Text","text":"ridee","type":"unstyled","depth":0,"inlineStyleRanges “: :

我按预期得到了这个,但是 convertFromRaw 给出了这个错误

无法将 undefined 或 null 转换为对象

Object.keys(entityMap).forEach(function (storageKey)

提前致谢!

【问题讨论】:

    标签: javascript reactjs html-editor draftjs


    【解决方案1】:

    我以错误的格式发送到我的服务器 我改变了 应用程序/x-www-form-urlencoded 到 应用程序/json

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 2020-07-19
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 2023-02-06
      • 2021-12-07
      • 2020-08-15
      相关资源
      最近更新 更多