当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 使用fetch进行数据请求时报json错误

使用fetch进行数据请求时报json错误

2020年01月08日  | 移动技术网IT编程  | 我要评论

使用fetch进行数据请求返回response对象,response.json报错。原因是response中含有特殊字符。

fetch(url).then(response => response.json())
  .then(data => console.log(data))
  .catch(e => console.log("oops, error", e))

取消response.json()调用,使用response.text()返回请求数据的字符串,对特殊字符进行转义替换处理后,再进行json对象化传入请求成功的回调函数中。transspecialchar是对字符串处理的函数

interface body {
    readonly body: readablestream<uint8array> | null;
    readonly bodyused: boolean;
    arraybuffer(): promise<arraybuffer>;
    blob(): promise<blob>;
    formdata(): promise<formdata>;
    json(): promise<any>;
    text(): promise<string>;
}
response.text().then((text) => {
    const json = json.parse(this.transspecialchar(text));
    successcallback(json);
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网