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
update:
Thank for the fix for this one sadly nothing seems to be displayed so assume it is the wording i’m using.
"status": "success",
"eventCount": 681,
"events": [
{
"baseUrl":
"pageUrl":
"name":
"owner":
"startDate":
"startTime":
"endDate":
"endTime":
These are the feed ids and the information can be pulled using this code
success: function(data, status) {
$('#content').empty();
var data = data.name;
alert(data);
for (var i=0;i < data;i++) {
$('#content').append(data[0].events);
};
well 1 out of 2 issues fixed so yay. Today not been a waste at least. Thank you so much for your help as well 🙂
What you have done is wrong,
$('#content').append('+data[0].events+');You should have done
$('#content').append(data[0].events);'+data[0].events+'is treated as a string and the output is as expected.