Using ajax: Json becomes undefined and unsure why on newest questions tagged ajax – Stack Overflow

I have a json feed which i’m currently using ajax to try and get the data displayed in a list format. I’ve never used a json feed before so had to learn alot from these forums.

At the moment the data is becoming undefined and not sure why. Now i know the json feed is across domain so need jsonp and a callback function as not one in url.

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSONP test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>

<body>
<div id="content"></div>
<script type="text/javascript">
function loadgatewayJSON() {

$.ajaxSetup({
        global  : true,
        dataType: 'jsonp',
        jsonp   : 'callback',
        cache   : true,
        data: {eventTypeId:1},
        ajaxStart: function(){
            //$('#content').hasClass('loading');
        },
        complete: function(){
             //$('#content').removeClass('loading');
        }
        });

    $.ajax({

        url: '(my url for feed)',
                   success: function(data, status) {
                        $('#content').empty();
                        var numResults = data.length;
                        alert(data);
                        for (var i=0;i < numResults;i++) {
                            $('#content').append("<p>"+data[0].eventCount+"</p>");

                        };

                   },
                   error:function (xhr, ajaxOptions, thrownError){
                    alert("An error has occured");
                    alert(xhr.status);
                    alert(xhr.responseText);
                }
        });
};
loadgatewayJSON();
</script>
<div id="content"></div>
</body>
</html>

I just wondered if anyone can spot any errors i’ve done in the code for reasons why it is pulling up undefined. I really just want a list of the events. I hope someone here could help me.

Update:

I’ve got the feed pulling now but struggling to pull the data within the json feed.

The stuff i need to pull is

“name”
“startdate”
“enddate”
“eventtype”

I assumed it was:

$('#content').empty();
var data = name.event;

alert(data);
for (var i=0;i < numResults;i++) {
$('#content').append('+data[0].events+');

Sadly all this pulls up is displaying +data[0].events+ load of times

help?

See Answers


source: http://stackoverflow.com/questions/11845952/json-becomes-undefined-and-unsure-why
Using ajax: using-ajax



online applications demo