本文介紹了從 JSON.parse 捕獲異常的正確方法的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我在有時(shí)包含 404 響應(yīng)的響應(yīng)上使用 JSON.parse
.在它返回 404 的情況下,有沒有辦法捕獲異常然后執(zhí)行一些其他代碼?
I’m using JSON.parse
on a response that sometimes contains a 404 response. In the cases where it returns 404, is there a way to catch an exception and then execute some other code?
data = JSON.parse(response, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new(window[type])(value);
}
}
return value;
});
推薦答案
我將某些內(nèi)容發(fā)布到 iframe 中,然后使用 json 解析讀回 iframe 的內(nèi)容...所以有時(shí)它不是 json 字符串
i post something into an iframe then read back the contents of the iframe with json parse...so sometimes it's not a json string
試試這個(gè):
if(response) {
try {
a = JSON.parse(response);
} catch(e) {
alert(e); // error in the above string (in this case, yes)!
}
}
這篇關(guān)于從 JSON.parse 捕獲異常的正確方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!