jquery - Getting empty Response from get request from Rest service in MVC 4 Asp.net -
i have simple method create list of objects team, convert json , returns it.
public actionresult index() { var teams = new list<team>() { new team{ name = "caracas f.c", city = "caracas", id = 1}, new team{ name = "millonarios", city = "bogota", id = 2}, new team{ name = "independiente", city = "bogota", id = 3}}; var jsondata = new { teamsdata = teams }; return json(jsondata, jsonrequestbehavior.allowget); }
when go url (http://prototypeteam.apphb.com/ , check out yourself), can see json
{"teamsdata":[{"id":1,"name":"caracas f.c","city":"caracas"},{"id":2,"name":"millonarios","city":"bogota"},{"id":3,"name":"independiente","city":"bogota"}]}
but when try json using jquery returns without response:
self.allteams = ko.observablearray(); $.getjson("http://prototypeteam.apphb.com/", function (data) { var mappedteams = $.map(data.teamsdata, function (item) { return new teamviewmodel(item); }); self.allteams(mappedteams); }); teamviewmodel = function (data) { self = this; self.id = data.id; self.name = data.name; self.city = data.city; }
this i'm getting using mozilla's firebug.
if can please me understand why happening. don't why see json @ url jquery seems not understand going on. thanks.
what you're seeing same origin policy.
javascript has security measure can't content external url's, can files ajax same domain.
there 2 excepions:
jsonp. jsonp isn't ajax, inserts script tag dom other script tag, avoids same origin policy. service has support , wrap json string inside function executes once script has loaded.
cors. enabling cors headers lets use scripts other domains, again has set on serverside.
there's option use service yahoo yql or pipes, gets content , wraps in either jsonp or adds cors header, uses 1 of 2 options above.
the same origin policy applies everwhere, if you're running on localhost , try file web, same origin policy won't allow it, url's don't match.
Comments
Post a Comment