1

as a new bee in Development, i have a question how can i get data according to my requeriment of what is the best practic

My design is like this.

Java script (Ajax call) >> ashx handler (Hit databse and return data) >> databse (my values)

i need data like this in order to render in HTML

 var events_array = new Array();
            events_array[0] = {
                startDate: new Date(2013, 01, 25),
                endDate: new Date(2013, 01, 25),
                title: "Event 2013, 01, 25",
                description: "Description 2013, 01, 25",
                priority: 1, // 1 = Low, 2 = Medium, 3 = Urgent
                frecuency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly
            };

            events_array[1] = {
                startDate: new Date(2013, 01, 24),
                endDate: new Date(2013, 01, 24),
                title: "Event 2013, 01, 24",
                description: "Description 2013, 01, 24",
                priority: 2, // 1 = Low, 2 = Medium, 3 = Urgent
                frecuency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly
            }

            events_array[2] = {
                startDate: new Date(2013, 01, 07),
                endDate: new Date(2013, 01, 07),
                title: "Event 2013, 01, 07",
                description: "2013, 01, 07",
                priority: 3, // 1 = Low, 2 = Medium, 3 = Urgent
                frecuency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly
            }

i want to know how can i send data like this from my ashx handler.

I have a class to EventEnfo. Can i pass list of EventInfo from handler and format/convert this in array like above? ? Any example please?

2 Answers 2

2

You could use a JavaScriptSerializer. So you could start by designing a model that will match the desired JSON structure:

public class EventInfo
{
    public DateTime startDate { get; set; }
    public DateTime endDate { get; set; }
    public string title { get; set; }
    ...
}

and then inside your handler:

public void ProcessRequest(HttpContext context)
{ 
    IEnumerable<EventInfo> result = ... fetch from db
    var serializer = new JavaScriptSerializer();
    context.Response.ContentType = "application/json";
    context.Response.Write(serializer.Serialize(result));
}

UPDATE:

And here's how you could consume the results:

$.ajax({
    url: '/myhandler.ashx',
    success: function(events) {
        $.each(events, function() {
            alert('Title of the event: ' + this.title);
        })
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes i did that and successded. just wana know how can i iterate on that data return my handler to construct array like that?
i can see values by doing this result[0].title
You could use the $.each method to loop through the results returned by the handler. I have updated my answer to shown an example.
2

events_array is not an array its an object, so doing new Array is wrong. do new Object or better {}:

var events_array = {};
events_array[0] = {...

if your backend can convert stuff into an JSON object you can send it via ajax to the client-side and parse it

JSON.parse(obj);

2 Comments

m using this JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); string serEmployee = javaScriptSerializer.Serialize(employee); context.Response.ContentType = "text/html";\ context.Response.Write(serEmployee); from backend when returning response to ajax call. Dont know you to fill that array then
context.Response.ContentType must be "application/json" then!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.