PDA

View Full Version : a bit of help with jsonp



fgeorge
28 Dec 2010, 1:23 PM
hello ,
i am trying to read out some json data from a url but it doesnt seem to be working.

see my code below.



Ext.setup({
onReady: function() {

var topgainers = new Ext.Component({
title: 'Timeline',
style: "background-color: red; color: white;",
scroll: 'vertical',
tpl: [
'<tpl for=".">',
'<div class="tweet">',
'<div class="avatar">Top Gainers</div>',
'<div class="tweet-content">',
'<h2>{gainers}</h2>',
'<p>{date}</p>',
'<p>{open}</p>',
'<p>{close}</p>',
'<p>{change}</p>',
'</div>',
'</div>',
'</tpl>'
]
});

Ext.util.JSONP.request({
url: 'http://www.xyz.com/topgainers',
callbackKey: 'callback',
callback: function(data) {
if (data && data.results && !!data.results.length) {
data = data.results;

// Update the tweets in timeline
topgainers.update(data);
} else {
topgainers.getContentTarget().update('No Results Available');
}
}
});

var red = {
dock: "top",
style: "background-color: #000000; color: white;",
title: "Daily Market List",
html: "Daily List",
flex: 1
};

var amber = {
dock: "top",
style: "background-color: #808080; color: white;",
title: "Top Gainers",
html: "Top Gainers",
flex: 1
};

var grey = new Ext.Carousel ({
items: [
{style: "background-color: black;"},
{style: "background-color: pink;"},
{style: "background-color: orange;"}
]
});

var rootPanel = new Ext.Panel({
fullscreen: true,
layout: {
type: "hbox",
align: "stretch",
pack: "end"
},
defaults: {flex: 1},
items: [topgainers, amber, grey],
dockedItems: [
{
style: "background-color: #000080; color: white;",
html: "XYZ Stockbrokers Limited"
}
]
})
}
});



The format of the date is;



[{"Topgainers":{"gainers":"CONOIL","date":"Dec 24 ","Open":34.71,"Close":36.44,"change":1.73}},{"Topgainers":{"gainers":"GUINNESS","date":"Dec 24 ","Open":189.00,"Close":190.56,"change":1.56}},{"Topgainers":{"gainers":"OKOMUOIL","date":"Dec 24 ","Open":13.87,"Close":14.50,"change":0.63}},{"Topgainers":{"gainers":"WAPCO","date":"Dec 24 ","Open":38.55,"Close":39.00,"change":0.45}},{"Topgainers":{"gainers":"UNILEVER","date":"Dec 24 ","Open":24.90,"Close":25.20,"change":0.30}}]


My code is not returning any data..Can anyone help here?

Thanks

doggroup
29 Dec 2010, 1:39 AM
Try to check json format
http://jsonformatter.curiousconcept.com/

mankz
29 Dec 2010, 1:43 AM
For JSONP to work, you have have to format the response as valid javascript, wrapped in a function named like the 'callback' parameter. Read more about JSONP here: http://soledadpenades.com/2009/07/29/jsonp-explained/