What am I doing wrong? Is there something wrong with the json returned from the Web Api service? Is there something I can do to provide more details about the error?
Your project seems to be returning message in correct format. However, you don't need to specify format when calling .getJSON method as the datatype is automatically set to JSON message. Try without specifying the format.
Is your jquery requesting that data from the same domain?
They are not on different domains, but they're on different ports on localhost. I just realised that if I put the requesting jquery in the same project (and on the same port) it works.
How do I allow the request to be on a different ports/domains?
GoranPersson
Member
311 Points
183 Posts
Can't get json with jquery
Feb 21, 2012 03:03 PM|LINK
I'm returning json from a Mvc project. When I capture the code with IE development tool I can see that the resonse body is
[{"ID":1,"Name":"School #1","Town":"Town"}, {"ID":3,"Name":"School #2","Town":"Town"}, {"ID":10,"Name":"School #3","Town":"Town"}]But when I try to get the json with jquery, nothing happends. This is my code:
$.getJSON("http://localhost:51491/api/schools", { format: "json" }, function (data) { $.each(data.items, function(i, item) { alert(item.Name); }); });If I change the url to
http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?
the jquery code works.
What am I doing wrong? Is there something wrong with the json returned from the Web Api service? Is there something I can do to provide more details about the error?
kushpaw
Member
512 Points
123 Posts
Re: Can't get json with jquery
Feb 21, 2012 03:20 PM|LINK
Your project seems to be returning message in correct format. However, you don't need to specify format when calling .getJSON method as the datatype is automatically set to JSON message. Try without specifying the format.
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: Can't get json with jquery
Feb 21, 2012 03:21 PM|LINK
add error code
http://bit.ly/mvc_ajax_jquery
maggie.ying
Member
183 Points
39 Posts
Microsoft
Re: Can't get json with jquery
Feb 21, 2012 03:41 PM|LINK
Is your jquery requesting that data from the same domain?
SixiS
Member
50 Points
16 Posts
Re: Can't get json with jquery
Feb 21, 2012 04:09 PM|LINK
GoranPersson
Member
311 Points
183 Posts
Re: Can't get json with jquery
Feb 22, 2012 12:31 PM|LINK
Thanks eveybody for trying to help me!
They are not on different domains, but they're on different ports on localhost. I just realised that if I put the requesting jquery in the same project (and on the same port) it works.
How do I allow the request to be on a different ports/domains?
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: Can't get json with jquery
Feb 22, 2012 01:10 PM|LINK
http://code.google.com/p/jquery-jsonp/
Christian.We...
Member
32 Points
11 Posts
Re: Can't get json with jquery
Feb 22, 2012 01:59 PM|LINK
You could apply JSONP - or enable CORS.
Both are illustrated here in samples: https://github.com/ChristianWeyer/Thinktecture.Web.Http
Microsoft MVP for Windows Azure [Architecture]
http://weblogs.thinktecture.com/cweyer
https://github.com/thinktecture/Thinktecture.Web.Http