无法从JSON获取数据

时间:2014-08-04 15:35:48

标签: jquery ajax json

我有json文件。 通过使用Ajax调用,我试图从Json文件中获取数据。

$( document ).ready(function() {
    $( "#day" ).click(function() {
        var actualDate = new Date(); // convert to actual date
        var date=actualDate.getDate();
        var month=actualDate.getMonth() +1;
        var year= actualDate.getFullYear();
        var today=date+"-"+month+"-"+year;
        alert(today);

        startJsonSession();
        return false;
    });

    function  startJsonSession(){
        alert("Am IN startJsonSession Method!!!!");

        $.ajax({
            url: "text.json",
            cache: false,
            contentType: "json",
            success: function(data) {
                $.each(data, function(index, value) {
                    alert(index + ': ' + value.Date);
                });
            }
        });
    }
});

Json文件是:

[
    {
        "Date": "04/08/2014",
        "EventName": "Metting",
        "EventDescription": "abcfdggf",
        "Image": ""
    },
    {
        "Date": "04/08/2014",
        "EventName": "Public Meeting",
        "EventDescription": "xyz",
        "Image": ""
    }
]

你可以建议我......

1 个答案:

答案 0 :(得分:0)

使用 getJSON 这与我测试过的ur json文件一起使用

$.getJSON('text.json', function (data) {

                 $.each( data, function( key, val ) {
                console.log(val.EventName );
                });

            });