سؤال

I running the following code, that has within also some quoted attempts that lead to failures:

$.post('Controller.php',
    {
        action: 'get_events'    
    },
    function(data, textStatus) {
        //var eventsInline = JSON.stringify(data); //Fails but comes in good direction.
        console.log(JSON.stringify(data));

        //This works, it is default
        var eventsInline = [{
                    "date": "1394220775280", 
                    "type": "meeting", 
                    "title": "Project A meeting", 
                    "description": "Lorem Ipsum dolor set", 
                    "url": "http://www.event1.com/" }]
        /*
        //ofcourse this fails
        $.each( data, function( index, calendar){

                    "date": calendar.event_sdate, 
                    "type": calendar.event_type, 
                    "title": calendar.event_title, 
                    "description": calendar.event_description, 
                    "url": calendar.event_url 
        });
        */
    $("#eventCalendarInline").eventCalendar({
        jsonData: eventsInline,
        showDescription: true
    }); 

     $('#indicator').hide();

    }, 
    "json"      
);

As stated the default code works. The console.log output is as following:

"[{
"0":"1",
"1":"1394220775280",
"2":"1402891200000",
"3":"meeting",
"4":"Project A meeting",
"5":"Lorem Ipsum dolor set",
"6":"http://www.event1.com/",

"event_id":"1",
"event_sdate":"1394220775280",
"event_edate":"1402891200000",
"event_type":"meeting",
"event_title":"Project A meeting",
"event_description":"Lorem Ipsum dolor set",
"event_url":"http://www.event1.com/"
}]"

It seems harder as i thought.

هل كانت مفيدة؟

المحلول

    $.post('Controller.php', {
        action: 'get_events'
    }, function(data, textStatus) {
        //var eventsInline = JSON.stringify(data); //Fails but comes in good direction.
        console.log(JSON.stringify(data));

        // Define the data
        var the_data = [];
        $.each(data, function(index, calendar) {
            // Fill "the_data" with each entry
            the_data.push({
                date:           calendar.event_sdate, 
                type:           calendar.event_type, 
                title:          calendar.event_title, 
                description:    calendar.event_description, 
                url:            calendar.event_url 
            });
        });

        $("#eventCalendarInline").eventCalendar({
            jsonData:           the_data,
            showDescription:    true
        }); 

        $('#indicator').hide();
    }, 'json');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top