0

I am using the google news search api and get back data in json format. I am using ajax to get the data and parse through it.

This is my code:

function doAjax(query){
    alert(query);
    var target = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q="+query+"&rsz=large";
    $.ajax({
        url: target,
        cache: false,                   
        dataType:'jsonp',
        success: function(data) {
            //alert(data.responseData.results);
            for(var x = 0; x < 8; x++){
                var title = data.responseData.results[x].titleNoFormatting;
                //var content = data.responseData.results[x].content;
                //var image = data.responseData.results[x].image.url;

                $("#home").append(x+"---"+title+'<hr>');
            }
        },
        error: function(jxhr,e){
        alert(jxhr.status+" --- "+e.responseText);
        }
    });
}

When I run the code this way I get 8 titles. but when I uncomment this line var image = data.responseData.results[x].image.url; I get 3 or 4 results only. I investigated the data being retrieved from google I found that some results has no images. How can I check the json result if it has an image. I still want to display the title of the article even if there was no image.

2
  • 1
    image = data.responseData.results[x].image && data.responseData.results[x].image.url Commented Jan 23, 2014 at 16:36
  • var image= data.responseData.results[x].image; image= image && image.url; Commented Jan 23, 2014 at 16:38

4 Answers 4

1

you should check the image exists before get it's url

if(typeof(data.responseData.results[x].image) == "undefined"){
    alert('no image');
}else{
    var image = data.responseData.results[x].image.url;
}
Sign up to request clarification or add additional context in comments.

Comments

1

What does the response look like? Most browsers have developer tools that will let you see the requests and response. Likely, your loop is choking because data.responseData.results[x].image is undefined so data.responseData.result[x].image.url throws a type error (undefined doesn't have a url property).

To guard against this, just check for it like this:

if(data.responseData.result[x].image) {
    var image = data.responseData.result[x].image.url;
}

If data.responseData.result[x].image is undefined then it will evaluate as falsey and the body of the if won't be executed (and won't throw an error that breaks you out of the function).

Comments

1

You can check for image like

if (typeof data.responseData.image != 'undefined' && typeof data.responseData.image.url != 'undefined' ) {
    // set the image here
}

Comments

1

Typeof can be used to determine if a variable is defined or not.

Example

var var_one = "";

alert(typeof var_one); // alerts "string"
alert(typeof var_two); // alerts "undefined"
alert(typeof var_two == "undefined"); // alerts "true"

So you can specify:

if (typeof var_two != "undefined")
{
    alert("variable is undefined!");
}
else
{
    alert("variable is defined... use it!");
}

Taking the above into account, you could do:

var image = "";

if (typeof data.responseData.results[x].image.url != "undefined")
{
    image = data.responseData.results[x].image.url;

    // You can then specify where the image url will go.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.