-4

I can't find what is wrong. I use getJSON:

    $.getJSON(api,  
    {
        Content-Type : 'application/json'
    }).done(function( data ) 
    {
        $(data).each(function(key,val){
            console.log(val.calories)
        });

    }).fail( function(xhr, textStatus, errorThrown) {
                alert(xhr.responseText);              
    });

and console returns me:

SyntaxError: missing : after property id  

pointing to line

Content-Type : 'application/json'
4

1 回答 1

1

The actual error is that {} is an object literal (I like to think of those as dictionaries, key-value pairs). The notation of an object literal is as follow:

var literal = {
    key: value,
    anotherKey, anotherValue
};

The problem with your line of code is that the 'key' is Content-Type. However, a dash in the key is not allowed. You can fix it by putting quotes around it:

{
    'Content-Type': 'application/json'
}

But, as said in the comments, you really don't have to set that option here. So you can just remove the object literal.

于 2013-11-13T15:22:55.010 回答