tags:

views:

50

answers:

3

I was wondering if there's a way to get "event" from ajax success? For example:

UPDATE:

$.ajax({
        type: "GET",
        url: "/home/PersonInfo",
        dataType: "json",
        success: function(data) {
        // I would like to use "data" like data.name, is this possible?    
        }
    });

to

function MyEvents(start, end, callback) {
    $.ajax({
        type: "GET",
        url: "/home/GetInfo",
        dataType: "json",
        success: function(data) {
            alert(data[0].start);
            var events = [];

            var meeting = new Date((data[0].start).getFullYear(),
                         (data[0].start).getMonth(),
                         (data[0].start).getDate());

            while (meeting <= data[0].end) {
                events.push({
                    id: data[0].id,
                    title: data[0].title,
                    start: new Date(meeting.valueOf()),
                    allDay: false
                });
                // increase by one week
                meeting.setDate(meeting.getDate() + 7);
            }
            callback(events);
        }
    });
}

data[0].start).getFullYear() is not a function? this is the json returned: [{"start":1277985600,"end":1278158400}] Can anyone help?

A: 

Probably you have to use JQueri.getJSON() method, you can find documentation on jQuery Docs.

s.susini
A: 

"/home/PersonInfo" needs to return a JSON object. What is your server side code written in?

ThatSteveGuy
My server side code is written in C#
hersh
MVC or WebForms? If you are using MVC, look up JsonResult.
ThatSteveGuy
+3  A: 

The code you have should work, assuming /home/PersonInfo gives you valid JSON like this:

{"name":"My Persons Name"}

Then data.name will be "My Persons Name" in your callback.

As a side note, Firebug / Chrome's Developer Tools should allow you to look at the actual request and see the returned JSON as an object. Plus they give you handy utilities for debugging like console.dir(data) to print out the data.

gnarf
I've tried it before and data.name is undefined. But I was reading up on "getJSON" and tried data[0].name gave me the right data. I wonder why?
hersh
Look at your JSON being returned, It's probably `[{"name":"My Persons Name"}]`
gnarf