0

In school we use ASP.NET but normally I am a RoR developer.

I get my data from a WCF service. I have created this ASP.NET MVC 5 project. I created a controller that looks like this:

[HttpGet]
    public ActionResult GetMovies()
    {
        return Json(client.RetrieveAllFilms(), JsonRequestBehavior.AllowGet);
    }

And a on load jquery script that looks like this at the following moment:

<script>
    $(document).ready(function () {
        console.log("document ready");

        $.ajax(
            {
                type: "GET",
                dataType: 'json',
                url: "http://localhost:5348/movies/getmovies",
                data: "",
                success: function(data)
                {
                    alert(data.d);

                    var list = JSON.stringify(data.d);

                    alert(list);


                    for (var i = 0; i < list.length; i++) {
                        alert(list[i].Description);
                    }
                },
                error: function(data)
                {
                    alert("ERROR");
                }
            });
    });
</script>

The problem:

The problem is that when i say http://localhost:5348/movies/getmovies in the postman app then it gives me a lot of JSON but when I use the jquery script then I get an empty array which it my variable list.

Hope someone can help here.

Up front thanks ;)

4
  • Your method is returning what looks to be a collection (RetrieveAllFilms()?) and a collection does not contain a property names d (as in data.d). And its already json so trying to stringify it would also be an error. If you want to loop through your list then $.each(data, function(index, item) { console.log(item.Description); } Commented May 6, 2015 at 14:01
  • Looks like you stringifyied d (then trying to iterate through a string) Commented May 6, 2015 at 14:05
  • First of all...did you include the jquery lib on your test page?? Commented May 6, 2015 at 14:09
  • @StephenMuecke you should have placed it as an answer. This helped with the problem. Commented May 6, 2015 at 14:09

1 Answer 1

0

Assuming your client.RetrieveAllFilms() method is returning a collection of objects that contain a property named Description then the script should be

$.ajax(
{
    type: "GET",
    dataType: 'json',
    url: '@Url.Action("getmovies", "movies")', // Do not hard code!
    data: "",
    success: function(data) {
        // data contains the collection as json, so iterate through data
        $.each(data, function(index, item) {
            // access properties of each item
            console.log(item.Description);
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.