2

I'm using asp mvc and I'm getting data in one part of my program like this

 public List<IncidentPerAreaCount> getIncident()
    {
        int RondeboschCounter = 0;
        int ClaremontCounter = 0;
        int AthloneCounter = 0;
        List<IncidentPerAreaCount> IncidentAreaCount = new List<IncidentPerAreaCount>();
        IncidentPerAreaCount Rondebosch = new IncidentPerAreaCount();
        IncidentPerAreaCount Claremont = new IncidentPerAreaCount();
        IncidentPerAreaCount Athlone = new IncidentPerAreaCount();

        List<Report> Reports = GetReports();
        for (int i = 0; i < Reports.Count(); i++)
        {
            if (Reports.AsEnumerable().ElementAt(i).Area == "Rondebosch")
            {
                RondeboschCounter++;
            }
            else if (Reports.AsEnumerable().ElementAt(i).Area == "Claremont")
            {
                ClaremontCounter++;
            }
            else if (Reports.AsEnumerable().ElementAt(i).Area == "Athlone")
            {
                AthloneCounter++;
            }

        }
        Rondebosch.AreaName = "Rondebosch";
        Rondebosch.NumberOfIncidents = RondeboschCounter;
        Claremont.AreaName = "Claremont";
        Claremont.NumberOfIncidents = ClaremontCounter;
        Athlone.AreaName = "Athlone";
        Athlone.NumberOfIncidents = AthloneCounter;

        IncidentAreaCount.Add(Rondebosch);
        IncidentAreaCount.Add(Claremont);
        IncidentAreaCount.Add(Athlone);

        return IncidentAreaCount;
    }

Then I'm trying to get this string via Jquery

 var Reports = [];
    $.ajax({
    url: "Home/getIncident",
    async: false,
    dataType: 'json',
    success: function (json) { Reports = json.whatever; }
    });
    alert(Reports);

However the alert function keeps coming up empty (ie empty textbox) instead of having a json formatted string with data.

Please help...

1
  • 1
    Of course it does. Welcome to the world of AJAX Commented Jun 11, 2012 at 16:38

3 Answers 3

1

You are putting the alert in the wrong place.

$.ajax({
    url: "Home/getIncident",
    async: false,
    dataType: 'json',
    success: function (json) {
        Reports = json.whatever; 
        alert(Reports); // should be here.
    }
});

Read this and this before you jump into code.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried that, but the alert messagebox that pops up is still empty
1

You get your data inside success function of ajax instead of outside of ajax. try to move alert inside success then you will get your data.

var Reports = [];
        $.ajax({
        url: "Home/getIncident",
        async: false,
        dataType: 'json',
        success: function (json) { 
                 Reports = json.whatever; 
                 alert(Reports); //Right place
        }
        });
        alert(Reports); // Wrong place

2 Comments

Seems like the right place to me. Async is set to false and Reports is a variable with scope beyond the jquery ajax method.
if I code "Reports = json" then the output in the alert messagebox is "[object Object],[object Object],[object Object]"
0

the first thing i see is that you are not serializing the object to return. you could do it like that

 return new JavaScriptSerializer().Serialize(your_object);

and in the client side you must to convert json string to a valid Js object, i do that using "d" atribute in the json response string

var theObject = $.parseJSON(response.d);

and theObject has the atributes you need.

and finally i see your object is a list, you can iterate using $.each

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.