switz
switz

Reputation: 25228

Catch $.getJSON error

I've been trying to figure this out for hours. I have a DYNAMIC youtube search, which I use Youtube's JSON api for. It works usually, but there are times that it won't find anything. Is there a way to figure out if it finds nothing, and then end the function because otherwise it stops the entire code. I tried jsonp, but that didn't seem to be correct. Somewhere I read that error catching is built into the newest jQuery getJSON, but I couldn't find it.

The code is really tedious so I'd rather not post it unless it comes to that. I'd appreciate any help! Thanks guys.

error showing that json didn't return anything

jquery-1.4.4.min.js:32 TypeError: Result of expression 'j' [undefined] is not an object.

http://pastebin.com/4rVjAUwa

Upvotes: 2

Views: 4336

Answers (4)

imez
imez

Reputation: 308

sorry but I have not privilege to comment. You cannot extract data from remote server with json. have you tried jsonp? it is

http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&q=phish&callback=?

Upvotes: 0

switz
switz

Reputation: 25228

I figured it out, all I had to do was:

if (data.feed.entry == null)

before the .each() to catch the error.

Upvotes: 0

Egor4eg
Egor4eg

Reputation: 2708

Did you tried just using of jsonp or using of jQuery.get and then parse output if it is not nothing?

Upvotes: 1

mdrg
mdrg

Reputation: 3412

If you need to catch error situations, use $.ajax with error property:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback,
  error: callback
});

Upvotes: 1

Related Questions