我正在使用JSON.parse有时包含404响应的响应.在它返回404的情况下,有没有办法捕获异常然后执行其他一些代码?
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;
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取登录到我的应用程序的用户的facebook个人资料图片.Facebook的API声明http://graph.facebook.com/517267866/?fields=picture返回正确的URL作为JSON对象.
我想从我的代码中获取图片的URL.我试过以下但我在这里遗漏了一些东西.
var url = 'http://graph.facebook.com/517267866/?fields=picture';
http.get(url, function(res) {
var fbResponse = JSON.parse(res)
console.log("Got response: " + fbResponse.picture);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
Run Code Online (Sandbox Code Playgroud)
运行此代码会导致以下结果:
undefined:1
^
SyntaxError: Unexpected token o
at Object.parse (native)
Run Code Online (Sandbox Code Playgroud)