Here is my code
var url = URL;
var imageURL = '';
$.getJSON("https://graph.facebook.com/?id="+encodeURIComponent(url)+"&scrape=true&method=post",
function (data) {
json_data = JSON.stringify(data);
json_data = json_data.replace(/\s+/g, ' ');
var obj = jQuery.parseJSON(json_data);
imageURL = obj.image[0].url;
alert(imageURL+'Facebook');
if(imageURL == ''){
$.getJSON("//query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20html%20WHERE%20url=%27"+encodeURIComponent(url)+"%27%20AND%20xpath=%27descendant-or-self::meta%27&format=json&callback=?"
, function(data) {
var res = $.grep(data.query.results.meta, function(image, key) {
return image.hasOwnProperty("property") && image.property === "og:image"
});
if (res.length > 0) {
var imageURL = res[0].content;
alert(imageURL+'Pinterest');
});
}
});
Now, most of the times it works. But in some cases like any URL from Phandroid. For example, http://phandroid.com/2015/09/25/galaxy-s7-february/
First method can't get obj.image[0].url;
because the JSON object does not exist. Since, imageURL was initially a ''
. So, the if condition should execute but it does not. I don't get alerts from any block in this case. How, should I proceed?