Daarwin
Daarwin

Reputation: 3014

Get items from jQuery ajax call

Im trying to learn jQuery Ajax/Json. Im trying to get the server to return a set of objets from a file. The call succeds but there are no objects (or i dont understand how to get them). It seems like zero objects are returned even if i can see that the controller method returna all the objects from the json file.

Here is the code with all the json objects: http://pastebin.com/86dpFa2W

//JAVASCRIPT

$.ajax({
    url: "/Home/GetFriends",
    contentType: "application/json; charset=utf-8",

    dataType: "json",
    success: getFriendsSuccess,
    error: getFriendsError,
}).done(function (data) {
    if( jQuery.isEmptyObject(data)){
                alert('empty');
            } else {
                alert('not empty');
            }
    alert(' wuhuuu ' + data.length);
});

//MVC CONTROLLER

  public JsonResult GetFriends()
        {
        var friends = new List<Friend>();
        var json = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/content/friends.json"));

        var playerList = JsonConvert.DeserializeObject<List<Friend>>(json);
        return Json(friends, JsonRequestBehavior.AllowGet);
    }

Upvotes: 0

Views: 110

Answers (1)

Steve Danner
Steve Danner

Reputation: 22158

You are just passing the wrong variable into the Json method.

Try changing your final line to this:

return Json(playerList, JsonRequestBehavior.AllowGet);

Upvotes: 2

Related Questions