json字符串格式

之前我是把json字符串这样传给前端

res.write("{'ok':true,'msg':'注册成功'}")

前端转化

fetch(`/json`,config)
                .then(response=>{
                    return response.json()
                })
                .then(data=>{
                    console.log(data)                 
                })

发现前端报错
Uncaught (in promise) SyntaxError: Unexpected token ’ in JSON at position 1

我一个以为是fetch没有配置请求设置,排查了半天,才发现是json字符串格式也是很严格的

比如如下,也是不行的,报同样的错误

JSON.parse("{'ok':true,'msg':'注册成功'}")

正确的json字符串的格式,前端报错消失

res.write('{"ok":true,"msg":"注册成功"}')

猜你喜欢

转载自blog.csdn.net/qq_37854055/article/details/80497737