jQuery/Json - Cannot convert 'data' to object -
i've simple ajax/json request jquery:
$.ajax({ type: "post", url: "/some-json-url", data: "score=" + 1, datatype: 'json', success: function(data){ if(data.msg){ alert(data.msg); } } });
however, if msg not set, gen error (looking through opera dragonfly):
unhandled error: cannot convert 'data' object
how can check if exists or not... in valid way?
if problem data
being null can check so:
if(data && data.msg){ //... }
or if have multiple properties, either this:
if(data){ if(data.msg){ //... } }
or return early:
if(!data) return; if(data.msg){ //... }
Comments
Post a Comment