天天看点

Unexpected token c in JSON at position 0 报错信息及解决

问题产生:mpvue编译过程报错 Unexpected token c in JSON at position 0,且是websocket返回实时数据时。

Unexpected token c in JSON at position 0 报错信息及解决
isJson(str) {
        if (typeof str == 'string') {
          try {
            var obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
              return true;
            } else {
              return false;
            }

          } catch (e) {
            console.log('error:' + str + '!!!' + e);
            return false;
          }
        }
      },      
if(_this.isJson(res.data)){
    const data = JSON.parse(res.data);
    let jsonObj = data.data;

    。。。。。其余代码省略,多加个判断就行
}