使用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); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/6714.html
標籤:其他
上一篇:WebApp開發-Zepto
下一篇:JavaScript 物件
